if else
sh的流程控制不可为空
1.if
if 语句语法格式:
if condition
then
command1
command2
...
commandN
fi
2.if else
if else 语法格式:
if condition
then
command1
command2
...
commandN
else
command
fi
if-elif-else 语法格式:
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
以下实例判断两个变量是否相等:
a=10
b=20
if [ $a == $b ]
then
echo "a == b"
elif [ $a -gt $b ]
then
echo "a > b"
elif [ $a -lt $b ]
then
echo "a < b"
else
echo "Ineligible"
fi
输出结果:
a < b
if else语句经常与test命令结合使用
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'Two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
输出结果:
Two numbers are equal!