Conditional judgment in shell programming
Conditions
If-then
Case
If-then
Single condition
if command
Then
Commands
Fi
When the command return code is 0 o'clock, the condition is set
if.sh
#! /bin/Bashif Date Then Echo "command EXEC"fiifdate123 Then Echo "Command EXEC1"fiEcho "Out if"[[Email protected]SH]# ./if.SHSaturday, October 29, 2016Ten: the: -Edtcommand exec./if.SH: Line8: Date123:command not foundoutif
Full Coverage
if command
Then
Commands
Else
Commands
Fi
if.sh
#! /bin/BashifDate1 Then Echo "Command" Echo "OK"Else Echo "Commond1" Echo "fail"fi[[Email protected]SH]# ./if.SH./if.SH: Line2: Date1:command not Foundcommond1fail
Conditional nesting
If Command1
Then
Commands
If Command2
Then
Commands
Fi
Commands
Fi
Multi-criteria
If Command1
Then
Commands
Elif Command2
Then
Commands
Else
Commands
Fi
Test command:
The first form of
if test condition
Then
commands
fi
The second form of
Brackets []:
if [condition]
Then
commands
fi
Compound condition judgment
[Condition1] && [Condition2]
[Condition1] | | [Condition2]
Three types of conditions
Numerical comparison
string comparison
File comparison
if.sh
Echoplese input a numread numif[$num-GTTen ] Then Echo "The num>10"fiif[$num-ltTen] && [$num-gt5 ] Then Echo "The 5<num<10"fi
[[Email protected]SH]# ./if.SHplese input a num OneThe num>Ten
Cond
Conditional judgment in shell programming (Shell 05)