Like other advanced programming languages, Shell provides commands used to control program execution processes, including conditional branches and loop structures. you can use these commands to create very complex programs.
Like other advanced programming languages, Shell provides commands used to control program execution processes, including conditional branches and loop structures. you can use these commands to create very complex programs.
Unlike traditional languages, Shell is used to specify condition values instead of Boolean operations, but commands and strings.
1. Test Command
The test command is used to check whether a condition is true. it can be used to test values, characters, and files. The test characters and corresponding functions are as follows.
(1) numerical test:
-If eq is equal to, it is true. -If the ne value is not equal to the ne value, it is true. -A value greater than-gt is true. -If ge is greater than or equal to, it is true. -If it is less than, it is true. -If the value of-le is less than or equal to, it is true.
(2) string test:
= Equals to true .! = Not equal to true. The-z string length is false. -If the length of the n string is not pseudo, it is true.
(3) file test:
-E: the file name is true if the file exists. -R: the file name is true if the file exists and is readable. -W: the file name is true if the file exists and can be written. -X: the file name is true if the file exists and can be executed. -S file name is true if the file exists and contains at least one character. -D. the file name is true if the file exists and is a directory. -F: the file name is true if the file exists and is a normal file. -C: The file name is true if the file exists and is a special character file. -B: the file name is true if the file exists and is a special file.
In addition, Linux also provides (!) , Or (-o), non-(-a) logical operators, used to connect test conditions, the priority is :! Highest, followed by-a and-o.
Bash can also perform simple arithmetic operations in the following format:
$ [Expression]
For example:
Var1 = 2
Var2 = $[var1*10 + 1]
Then the value of var2 is 21.
2. if condition statement
The condition branch in the Shell program is implemented through the if condition statement. The general format is:
- If condition command string
- Then
- Command string when the condition is true
- Else
- Command string when the condition is false
- Fi
3. for loop
The for loop executes a command sequence for all possible values of a variable. The values assigned to the variable can be provided in the form of a numerical list in the program, or in the form of a positional parameter outside the program. The general format of the for loop is:
- For variable name [in value list]
- Do
- Several command lines
- Done
The variable name can be any string selected by the user. if the variable name is var, the value given after in Replaces $ var in the circular command list in sequence. If in is omitted, the value of var is the location parameter. Every possible value assignment to a variable will execute the command list between do and done.
4. while and until loops
Both the while and until commands use the return status value of the command to control the loop. The general format of the While loop is:
- While
- Several command lines 1
- Do
- Several command lines 2
- Done
As long as the return status of the last command in "several command line 1" in while is true, the while loop continues to execute "several command line 2" between do... done ".
The until command is another loop structure, which is similar to the while command in the following format:
- Until
- Several command lines 1
- Do
- Several command lines 2
- Done
The difference between an until loop and a while loop is that a while loop continues to run when the condition is true, while an until loop continues to run when the condition is false.
Shell also provides the "true" and "false" commands to create an infinite loop structure. Their Return states are always 0 or not 0.
5. signal
The trap command is used to capture signals in Shell programs. There are three response methods:
(1) execute a program to process this signal.
(2) receive the default signal operation.
(3) ignore this signal.
Trap provides three basic forms for the above three methods:
The first form of trap command will execute the command string in double quotation marks when Shell receives the signal with the same value as the signal list.
- Trap 'commands' signal-list
- Trap "commands" signal-list
To restore the default signal, use the second form of trap command:
Trap signal-list
The third form of the trap command allows the ignore signal:
Trap "" signal-list
Note:
(1) the signal 11 (segment violation) cannot be captured, because the Shell itself needs to capture the signal for memory dump.
(2) in the trap, you can define the processing of the signal 0 (in fact, there is no such signal). The Shell program sends this signal when it terminates (such as executing the exit statement.
(3) after capturing the signal specified in the signal-list and executing the corresponding commands, if these commands do not terminate the Shell program, the Shell program will continue to execute the command after the command received when the signal is received, which will easily cause the Shell program to be unable to terminate.
In the trap statement, the single quotes and double quotation marks are different. when the Shell program first encounters the trap statement, it will scan the commands in commands, if commands is enclosed in single quotes, Shell will not replace the variables and commands in commands; otherwise, the variables and commands in commands will be replaced with the specific values at that time.