Mainly introduces the syntax of Shell Basic statement
If statement syntax
1 single branch structure (if, then)
If < condition test >; then instruction; fi
Or as follows:
If < condition testing >
? Then
? Instructions
Fi
2 Dual-branch structure (if, then, otherwise ...) )
If < conditional expressions >
? Then
? Instructions
Else
? Instructions
Fi
Multi-branched structure (if, then, otherwise if, then, otherwise ...) )
If < conditional expressions >
? Then
? Instructions
elif < conditional expressions >
? Then
? Instructions
Else
? Instructions
Fi
Where elif can have multiple
Case syntax
Case "Variable" in
? value 1)
? Directive 1 ...
? ;;
? value 2)
? Directive 2 ...
? ;;
?*)
? Directive 3 ...
Esac
When the value of the variable is value 1 o'clock, execution of Directive 1, and so on, does not conform to the instruction of last *)
While statement
While < conditional expressions >
Do
? directive:
Done
Description
1 while on condition detection, if the execution command is set, done, and then detected, if established, and executed again until the condition is not met, stop the loop
2 while ture indicates that the condition is always true and will always loop (while [1] and while ture)
How the while loop reads files by row
Method One
exec < file
While Read line
Do
? Instructions
Done
Method Two
Cat File|while Read Line
Do
? Instructions
Done
Method Three
While Read line
Do
? Instructions
Done<file
Until syntax
Until < conditional expressions >
Do
? directive:
Done
Note: The condition will not be established, the execution of the cycle, the condition is set to stop
For syntax
1 for variable name in variable list
Do
? directive:
Done
2 for ((EXP1;EXP2;EXP3))
Do
? directive:
Done
Description
The for is followed by 3 expressions, the first is the variable initialization, the second is the range of the variable, the third is the variable self-addition or decrement, and when the first initialization value conforms to the second variable range, the loop, otherwise, exits.
**select statements
Select variable name [in menu Value list]
Do
? Instructions..
Done
Shell programming If syntax, case syntax, while statement, until statement, for statement, select statement