if...elif...fi 语句是提前一个级别的控制语句,形式出几个条件,允许 Shell 作出正确的决定。
语法
if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi
这段代码没有什么特别的。这仅仅是一个系列,if 语句每一个语句else子句的一部分。下面语句是执行的基础上的真实情况,如果条件不为 ture,则执行else块。
例子:
#!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" elif [ $a -gt $b ] then echo "a is greater than b" elif [ $a -lt $b ] then echo "a is less than b" else echo "None of the condition met" fi
这将产生以下结果:
a is less than b