遍匈 / Shell縮殻 /

Shell if else囂鞘

<貧匯准 和匯准>
if 囂鞘宥狛購狼塰麻憲登僅燕器塀議寔邪栖畳協峇佩陳倖蛍屶。Shell 嗤眉嶽 if ... else 囂鞘?
  • if ... fi 囂鞘?
  • if ... else ... fi 囂鞘?
  • if ... elif ... else ... fi 囂鞘。

1) if ... else 囂鞘

if ... else 囂鞘議囂隈?
if [ expression ]
then
 Statement(s) to be executed if expression is true
fi
泌惚 expression 卦指 true?then 朔円議囂鞘繍氏瓜峇佩?泌惚卦指 false?音氏峇佩販採囂鞘。

恷朔駅倬參 fi 栖潤硫液栽 if?fi 祥頁 if 宜狛栖憧亟?朔中匆氏囑需。

廣吭?expression 才圭凄催([ ])岻寂駅倬嗤腎鯉?倦夸氏嗤囂隈危列。

訟倖箭徨?
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
 echo "a is equal to b"
fi
if [ $a != $b ]
then
 echo "a is not equal to b"
fi
塰佩潤惚?
a is not equal to b

2) 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
泌惚 expression 卦指 true?椎担 then 朔円議囂鞘繍氏瓜峇佩?倦夸?峇佩 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

3) if ... elif ... fi 囂鞘

if ... elif ... fi 囂鞘辛參斤謹倖訳周序佩登僅?囂隈葎?
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
陳匯倖 expression 議峙葎 true?祥峇佩陳倖 expression 朔中議囂鞘?泌惚脅葎 false?椎担音峇佩販採囂鞘。

訟倖箭徨?
#!/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

if ... else 囂鞘匆辛參亟撹匯佩?參凋綜議圭塀栖塰佩?崧宸劔?
if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

if ... else 囂鞘匆将械嚥 test 凋綜潤栽聞喘?泌和侭幣?
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
 echo 'The two numbers are equal!'
else
 echo 'The two numbers are not equal!'
fi
補竃?
The two numbers are equal!
test 凋綜喘噐殊臥蝶倖訳周頁倦撹羨?嚥圭凄催([ ])窃貌。
<貧匯准 和匯准>

AltStyle によって変換されたページ (->オリジナル) /