Linux shell script series tutorial (I): getting started with shell

Time:2022-1-24

1、 Shell introduction

The design of many UNIX like operating systems is amazing. Even today, decades later, the UNIX style operating system architecture is still one of the best designs ever. One of the most important features of this architecture is the command line interface or shell. The shell environment enables users to interact with the core functions of the operating system. The term script refers more to this environment. Scripts are usually written in an interpreter based programming language. Shell scripts are essentially text files. We can write a series of commands to be executed, and then execute them through the shell.

Here we introduce bash shell (Bourne again shell), which is the default shell environment of most gun / Linux systems. All the experiments in the book are in Ubuntu 14 Completed in 04 lts environment.

2、 Basic operation

1. Open the terminal

In Ubuntu 14 In the 04 lts system, a terminal has been installed by default. We can open the terminal in a variety of ways. Here are two:

Method 1:Through the system’s own retrieval system, we can easily find the terminal and click to open it. The retrieval system can be started through the button in the upper right corner of the quick start bar.
Method 2:In order to easily open the terminal later, it is recommended to fix the terminal in the quick start bar. The operation method is as follows: after opening the terminal through method 1, a terminal chart will appear in the quick start bar. Right click the chart and select “fixed in the start bar” to fix the terminal in the quick start bar.

2. Terminal initialization interface

By default, the terminal prompt is: [email protected] perhaps [email protected] # Represents ordinary user and # represents root user.
For example, after I open the terminal, the prompt is: [email protected] :~$。
Root is the user with the highest authority in the Linux system, with great ability and risk, so I don’t mind using root as the default user to log in to the system.

3. Switch users

Generally, for the personal Linux operating system, there are two users on the system, namely, the user himself and the root user. For users, it is necessary to switch users from time to time to perform some operations that ordinary users cannot perform. Two methods are provided here to switch users.
Method 1: temporary switching. As the name suggests, this switching method is only temporary. When the instruction is executed, it will switch to the original user. The switching instruction is sudo command, which is short for super user do.

Method 2: long-term switching. As the name suggests, after switching using this method, the instruction will not return to the ordinary user after execution. The switching instruction is Su, which is the abbreviation of switch user. After that, you will be prompted to enter a password to complete the user switching.

4. Shell script

The following script is used to print Hello world from the terminal! character string.

Copy codeThe code is as follows:
#!/bin/bash
echo “Hello World!”

The starting line of a shell script is usually #/ Bin / bash, where / bin / Bash is the path to the interpreter to interpret and execute subsequent commands. Commands are separated by line breaks or semicolons.

5. Run the script

In Ubuntu, there are several ways to run scripts.
Method 1: bash test SH, in this case, the first line of the script file does not have to be “#/ Bin / bash “, because the interpreter has been specified under this method.
Method 2: first modify the permissions of the script file Chmod a + X test SH, this instruction is mainly to grant executable permissions to script files. Then execute the file/ test. SH is enough; Alternatively, you can execute the script through the full path.

6. Script comments

Shell scripts are no exception. We need to provide comment lines in some places, so that the code is easy to understand# The following contents are comments and will not be interpreted. Note: # is a single line comment.

Recommended Today

JVM + GC parsing (premise knowledge concatenation)

Premise preparation JVM GC garbage collection JVM virtual machine monitoring, tuning and troubleshooting Tomcat and microservice optimization 1. Premise review 1.1. JVM memory structure 1.1.1、 JVM Architecture Overview The gray part in the figure isThread private, there is almost no garbage collectionOrange partThread sharing, the main place where garbage recycling occurs What is the class […]