Shell Programming
1. Briefly
The format for writing shell scripts using the vi text Editor is fixed, as follows:
#!/bin/bash
#comments
The symbol in the first line #! tells the system that the program that is specified by the following path bash is the shell program that interprets the script file.
Except for the first line, the line that begins with # is the comment line until the end of this row. If a row is not completed, you can add "at the end of the line", which indicates that the next row and this row will be merged into the same row. 2. Variable
There are environment variables, internal variables, user variables.
Shell script is a weakly typed language that does not need to declare its type first when using variables.
Variable_name = Variable_value
When you take a value, you add $ to the variable name, $variable _name can be used in quotes, which is significantly different from other high-level languages.
Special variables
$$
The shell itself PID (ProcessID)
$!
The PID of the Shell's last running background process
$?
End code of the last command to run (return value)
$#
Number of arguments added to the shell
$
File name of the shell itself
$1~ $n
The value of each parameter added to the shell. $ is the 1th parameter, $ is the 2nd parameter ....
Demo
3. Comparison of Numbers
-eq equal
-ge Greater or equal
-le Less or equal
-ne Not Equal
-GT Greater than
-lt less than 4. String comparison
= = is equal
Whether the!= is unequal
-N length is greater than 0
-Z length is equal to 0
5. File Operators
-D is a directory
Whether-f is a file
-R is readable
Whether-W can be written
-X to perform 6. Logical Operators
。 Equivalent C language.
-a equivalent C language &&
-o equivalence C language | | 7.for Statement
For current_variable
Todo
Statements
Done 8.if Statements
if [expression]; Then
Statements
elif [expression]; Then
Statements
Else
Statements
Fi
Description: Elif or ELSE statements are not required. 9.case Statement
Case STR in
str1 | STR2)
statements;;
STR3|STR4)
statements;;
*)
statements;;
Esac
Description: * Corresponding to the C language default; ";;" corresponding to the C language break. Exit Statement
Exit the shell program and return a value to facilitate invocation by another shell and give feedback. 11. Function
Func () {
Statements
}
Execute other shell script in the shell script 12.1 direct call like/root/soft/apache-tomcat-8.0.21/bin/shutdown.sh Run the time to open a Sub-shell execute the call script, Sub-shell after execution to return to Parent-shell. Sub-shell inherits environment variables from Parent-shell, but environment variables in Sub-shell are not brought back to Parent-shell.
12.2 exec like exec/root/soft/apache-tomcat-8.0.21/bin/shutdown.sh execute the target script under the current shell, after execution, The contents of the Exec line in the current script will not be executed again.
12.3 Source
such as source/root/soft/apache-tomcat-8.0.21/bin/shutdown.sh or. /root/soft/apache-tomcat-8.0.21/bin/shutdown.sh
The source command, also known as the Point command, is a dot symbol (.) and is the internal command for bash. The statements in the target script are sequentially executed in the current shell, and no new child shells are created. Then all the new, variable-changing statements in the script will be saved in the current shell.