shell-code-5-流程控制

××××××××××××××××××××IF-ELSE××××××××××××××××××××××××××××××

a=1
b=2
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 error
fi

#if-else常和test一起使用。

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo $num1 $num2
echo '两个数字相等!'
else
echo '两个数字不相等!'
fi

echo $[2*3]

# 写成一行if condition; then command1; fi

××××××××××××××××××××FOR××××××××××××××××××××××××××××××

# for var in item1 item2 ... itemN; do command1; command2… done;
# in列表可以包含替换、字符串和文件名。
# in列表是可选的,如果没有,for循环使用命令行的位置参数。
for i in 1 2 3
do
echo $i
done

××××××××××××××××××××WHILE××××××××××××××××××××××××××××

i=0
# 注意双层括号,空格
while (( $i<=5 ))
do
echo $i
# Bash let 命令,它用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量
let "i++"
done
# 使用read从键盘读入,存到变量pxy。不需要提前声明。
while read pxy
do
echo "是的!$pxy 是一部好电影"
done
# 无限循环
while :# while true
do
command
done
for (( ; ; ))

××××××××××××××××××××UNTIL××××××××××××××××××××××××××××

××××××××××××××××××××CASE××××××××××××××××××××××××××××

单行的命令可以和1)写在同一行。

××××××××××××××××××××jump out××××××××××××××××××××××××××××

# break 和continue类似c语言用法

while :
do
# -n means no newline
echo -n "输入 1 到 5 之间的数字:"
read aNum
case $aNum in
# attention!
1|2|3|4|5) echo "你输入的数字为 $aNum!"
;;
*) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
# jump out of while
break
;;
esac
done
echo "GAME OVER"

原文地址:https://www.cnblogs.com/pxy7896/p/6419041.html