Detailed explanation of basic operations of shell variables and common symbols

A shell script creation, permissions, execution

1.1 Creation of shell script

touch hellloworld.sh
or vi helloworld.sh

1.2 Grant execution permissions

chmod +x helloworld.sh

1.3 Execute script

command Comment
[root@localhost ~]# ./helloworld.sh Script file path (absolute path and relative path) ===>Executable permission required
[root@localhost ~]# sh helloworld.sh sh script file path ===> may not need execution permission
[root@localhost ~]# source helloworld.sh Source script file path ===> The path itself will change
[root@localhost ~]# . helloworld.sh Load script by dot

Two pipe symbol and redirection symbol

2.1 Pipeline operation

Pipeline operation provides a mechanism for collaborative work between different commands. The output result of the command on the left side of the pipe symbol "|" is used as the input (processing object) of the right command . Multiple pipes can be used in the same command line.
cmd command 1 | cmd command 2 | cmd command n
For example:
free -m | grep “Mem” | awk'{print $6}'

2.2 Redirection operation

Types of Operator use
Redirect input < Read data from the specified file instead of inputting from the keyboard
Redirect output > Save the output result to the specified file (overwrite the original content)
Redirect output >> Append the output to the end of the specified file
Standard error output 2> Save the error information to the specified file (overwrite the original content)
Standard error output 2>> Append the error message to the end of the specified file
Mixed output &> Save the standard output and standard error content to the specified file (overwrite the original content)
Mixed output &>> Append the standard output and standard error to the end of the specified file

Three shell variables

3.1 Variable types

Custom variables: defined, modified and used by the user.
Special variables: environment variables (USER HOME LANG PATH), read-only variables, location variables ($1, $2, $3...), predefined variables
Pre-defined variables:

variable Comment
$# The number of position variables in the command line
$* The contents of all location variables
$? The status returned after the last command is executed, the return status value is 0 means the execution is normal, non-zero value means abnormal or error
$0 The name of the currently executing process/program (ie return to the current program)
$@ Split all positional parameter lists
$* Treat all positional parameter lists as a whole

3.2 Points to note about variable assignment

When the variable name is easily confused with other characters immediately following it, you need to add braces "{}" to enclose the variable name , otherwise the correct variable name will not be displayed, and the undefined variable will be displayed as a null value.
Insert picture description here

3.2.1 Double quotes (")

It is mainly used to define a string. When the content to be assigned contains spaces, it must be enclosed in double quotation marks, and can be omitted in other cases.
Insert picture description here
You can use the "$" symbol to refer to the value of other variables within the range of double quotation marks.
Insert picture description here

3.2.2 Single quotation mark (')

Within the scope of single quotes, the values ​​of other variables cannot be quoted, and all characters are treated as ordinary characters.
Insert picture description here

3.2.3 Backtick (`)

The backtick must be a command line that can be executed, that is, input the content of the backtick as a command into the terminal, and then input the output result as a new command into the terminal.
Insert picture description here
In the figure, "expr" means calculation. The result of "12+13" is calculated, and "25" is obtained. When 25 is input as a command, the command cannot be found...
note:
It is difficult to nest back apostrophes in multiple levels, you can use "$()" instead.
E.g:Insert picture description here

3.2.4 read command

The read command is used to prompt the user to input information to implement a simple interactive process. When executing, read a line of content from the keyboard. Use the space as the separator to assign the read fields to the specified variables in turn.
Insert picture description here
The read command can be combined with the "-p" and "-t" options to set the prompt message and waiting time (the default is seconds).
For example: do
Insert picture description here
it again
Insert picture description here

3.3 Variable scope

By default, the newly defined variables are local variables and are only valid in the current environment. When entering the command "bash" to enter the subshell environment, local variables will not be available.
Insert picture description here
The specified variable can be exported as a "global variable" through the command "export", multiple variables can be operated at the same time, and the variable names are separated by spaces.
Insert picture description here

Guess you like

Origin blog.csdn.net/cenjeal/article/details/107952005