-
Shell control Structure: conditional structure, branching structure, cyclic structure
-
If, conditional structure
-
If expression then command table [else command table]fi you can use semicolons to combine multiple commands in one line. Therefore, in the aesthetic and space-saving considerations, the above format is abbreviated as: if expression; then command table [else command table]fi
-
If statements can be nested indefinitely
-
If expression then command table [elif expression then command table] ... [Else Command table]fi
-
You can change the direct command table to an IF condition statement
-
Case structure
-
Case expression in-mode 11[| mode 12] ...) command table 1;; mode 21[| mode 22] ...) command table 2;;.. *) command table n;; Esac
-
Example
-
1 #! /bin/bash 2 #filename: a.sh 3 Case $4 dir | Path) echo "Current path is ' pwd '";; 5 date | Time) echo "Today is ' date '";; 6 *) echo "Invalid argument!!" 7 echo "Enter argument:dir/path/date/time";; 8 ESAC
-
Case and if statements can be nested
-
Select structure
The Select Loop structure allows you to generate menus in a simpler way. The generated menu is numbered, and after the user enters the selected number, the corresponding code is executed according to the user's choice.
-
Select variable in list do command table done
-
List, which is a sequence of strings.
-
Using the Select structure will generate a digitized menu on the screen, prompting the user to make a selection, and the default prompt is #. The user simply needs to enter the corresponding number at the prompt to complete the selection.
-
Typically, select is used with the case structure, allows the user to select from the menu, and executes the corresponding command based on the selection.
-
The select structure is a loop that requires you to exit the loop using the break command, or you can use the Exit command to end the script.
-
Example
-
1 #! /bin/bash 2 #filename: a.sh 3 echo "---------Select one week plan---------" 4 select N in Mon Tue Wed Thu Fri Sat Sun 5 do 6 case $N in 7 mon) echo "Monday"; 8 tue) echo "Tuesday";; 9 wed) echo "Wednesday";; 10 thu) echo "Thursday";;  11   FRI) echo "Friday";; 12 sat) echo "Saturday";; 13 sun) echo "Sunday";; 14 *) echo "wrong" 15 break;; 16 esac 17 done
-
When you run the script with SH a.sh, the select:not found is displayed. Need to use bash a.sh to run scripts
-
# bash a.sh---------Select one week plan---------1) Mon2) Tue3) Wed4) Thu5) Fri6) Sat7) sun#? 1monday#?
-
While loop
-
While expression do command table done
-
While loops can be nested
-
Example
-
1 #! /bin/bash 2 #filename: a.sh 3 i=1 4 While [$i-le] 5 does 6 echo "i= $i" 7 i=$ (($i + 1)) 8 done
-
Note that there is a space behind [],[] after the while and there is a space in front of it, otherwise an error occurs.
-
For loop
-
For variable [in List]do command table done
-
Example
-
1 #! /bin/bash 2 #filename: a.sh 3 for I in 1 2 3 4 5 6 4 does 5 echo "i= $i" 6 i=$ (($i + 1)) 7 done
-
If the In list is omitted, for each positional parameter in the current execution script as a list, the for executes one of the list at a time. That is, the "for variable" implicitly means "for variable [email protected]".
-
Example (ARG, can be any name, not fixed)
-
1 #! /bin/bash 2 #filename: a.sh 3 i=1 4 for ARG 5 does 6 echo "i= $i, Parameters: $arg" 7 i=$ (($i + 1)) 8 done
-
Until cycle
-
Until is the continuation of the loop when the condition is false, and stops execution when the condition is true.
-
Until command table 1test expression do command table 2done
-
Example
-
1 #! /bin/bash 2 #filename: a.sh 3 i=1 4 echo "Please enter the number N:" 5 read n 6 until 7 test $i-ge $N 8 do 9 echo "I= $i" i=$ (($i + 1)) done
-
Break statement
-
Break statement, which can end the execution of a structure such as while, for, until, or select, which jumps out of the structure. After exiting the loop, it goes to the done statement and resumes execution.
-
If you have a multi-layered structure nested, you can also add a number after the break keyword to indicate the number of layers of the structure you want to jump out of. For example, break 2
-
Example
-
1 #! /bin/bash 2 #filename: a.sh 3 i=1 4 echo "Please enter the number N:" 5 read n 6 until 7 test $i-ge $N 8 do 9 if [$i-le 4];then echo "Hello" one-i=$ (($i + 1))-Else echo "i= $i" echo "World" 16 Fi + Done
-
Continue statements
-
The continue statement is the same as the break usage and is used to skip the remaining code in this loop.