if语法
if condition
then
command1
command2
...
commandN
fi
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
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
网友评论