The Shell has three kinds of if ... else statements:
1.if. FI statement;
2.if ... else ... fi statement;
3.if. Elif ... else .... fi statement.
If ... fi statement
Syntax for the IF ... else statement:
if [Expression] Then If Truefi
Note: There must be a space between expression and square brackets ([]), or there will be a syntax error.
If ... else ... fi statement
Syntax for the If ... else ... fi statement
if [Expression] Then If Trueelse If True fi
If ... elif ... else ... fi statement
Syntax for the If ... else ... elif. Fi statement
if[Expression1 ] ThenStatement (s) to be executedifExpression1Istrueelif[Expression2 ] ThenStatement (s) to be executedifExpression2Istrueelif[Expression3 ] ThenStatement (s) to be executedifExpression3IstrueElseStatement (s) to be executedifNo expression istruefi
If-else statement that mates with test
The IF ... Else statement is also often used in conjunction with the Test command, as follows:
num1=1num2=2if test ${num1}-eq ${num2}then Echo 'thenumbers is equal! ' Else Echo ' The numbers is not equal! ' fi
Shell script: If-else