Shell - if

Controlled conditions - if

if
supports nested
into a single line so you have to write: if []; then echo " "; fi
single if :( expression must be a space)

if []
then
cmd1
fi

Single if else :( expression must have a space, then there can not be after else)

If []
then
cmd1
else
cmd2
fi

if else-if else: (must have then after elif)

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

Example:

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
Published 544 original articles · won praise 289 · Views 230,000 +

Guess you like

Origin blog.csdn.net/BlessingXRY/article/details/100179913