Shell if...else...fi 語句

 if...else...fi 語句是控制語句,它允許下一個表格執行語句 Shell 更可控的方式在兩個選擇之間作出決定。

語法

if [ expression ] then Statement(s) to be executed if expression is true else Statement(s) to be executed if expression is not true fi

 Shell expression 求值。如果結果值是真實的,給定 statement(s) 被執行。如果表達式爲 false,則語句將不會被執行。 

例子:

如果我們把上面的例子中,那麼它可以用更好的方式使用 if...else 語句如下:

#!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" else echo "a is not equal to b" fi

這將產生以下結果:

a is not equal to b