if/elif/else结构
if/elif/else结构针对某一事件的多种情况进行处理。通常表现为“如果满足某种条件,则进行某种处理,否则接着判断另一个条件,直到找到满足的条件,然后执行相应的处理”。其语法格式为:
if expression1
then
command
command
…
elif expression2
then
command
command
…
elif expressionN
then
command
…
command
else
command
…
command
fi
vim ifelifelse_exam1.sh
#ifelifelse_exam1.sh
#!/bin/bash
echo "Please Input a integer(0-100): "
read score
if [ "$score" -lt 0 -o "$score" -gt 100 ]
then
echo "The score what you input is not integer or the score is not in (0-100)."
elif [ "$score" -ge 90 ]
then
echo "The grade is A!"
elif [ "$score" -ge 80 ]
then
echo "The grade is B!"
elif [ "$score" -ge 70 ]
then
echo "The grade is C!"
elif [ "$score" -ge 60 ]
then
echo "The grade is D!"
else
echo "The grade is E!"
fi
++++++++++++++++++++++++++++
vim love.sh
#!/bin/bash
read -p "请输入你是否爱我,我爱你|我不爱你:" love
if [ 我爱你 == "$love" ]
then
echo "我也爱你!"
elif [ 我不爱你 == "$love" ]
then
echo "我也不爱你!"
else
echo "请正确输入如:./$0 我爱你|我不爱你"
fi