Shell - if

条件控制 - if

if
支持嵌套
写成一行就得这么写:if [ ] ; then echo “” ;fi
单一的if:(表达式必须有空格)

if []
then
cmd1
fi

单一的if else :(表达式必须有空格,else后不能有then)

If []
then
cmd1
else
cmd2
fi

if else-if else :(elif后必须有then)

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 '没有符合条件'
fi
发布了544 篇原创文章 · 获赞 289 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/BlessingXRY/article/details/100179913