Unix-if-else-statement

提供:Dev Guides
移動先:案内検索

Unix/Linux Shell-if …​ else …​ fiステートメント

*if ... else ... fi* ステートメントは、シェルが制御された方法でステートメントを実行し、正しい選択を行えるようにする次の形式の制御ステートメントです。

構文

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_は、上記の構文で評価されます。 結果の値が_true_の場合、指定された_statement(s)_が実行されます。 _expression_が_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