天天看點

linux 分支語句(if、case)

linux 分支語句

******************************

分支語句:if

文法格式

if test選項; then
    commands;
fi


*****************************

if test選項; then
    commands;
else 
    commmands;
fi


*****************************

if test選項; then
    commands;
elseif test選項; then
    commands;
else
    commands;
fi
           

*******************

示例

count=1
if [[ $count -gt 2 ]]; then
    echo "$count 大于 2"
else
    echo "$count 小于等于 2"
fi

if(($count>2)); then
    echo "$count 大于 2";
else
    echo "$count 小于等于 2"
fi
           

運作腳本輸出

linux 分支語句(if、case)

******************************

分支語句:case

文法格式

case word in
    pattern) commands;
             ;;
    pattern) commands;
             ;;
    ....
esac
           

說明:pattern可為通配符表達式,找到第一個比對的即可退出

*******************

示例

foo="abc"
case $foo in
  a*) echo "$foo"
      ;;
  *) echo "hello world2"
      ;;
  ???) echo "hello world3"
esac
           

運作腳本輸出

linux 分支語句(if、case)

繼續閱讀