LInux:循环语句
-
if-else语句
-
if-else语句
-
for 循环
-
while 语句
-
无限循环
while True; doecho "ok" donefor ((::))
-
until循环
-
case … esac
echo "输入1到4之间的数字" read -p "数字:" num case $num in 1) echo '1';;2) echo '2';;3) echo '3';;4) echo '4';; esac s1='iambot' case $s1 in'iambot') echo 'bot';;'iampm') echo 'pm';;'iampl') echo 'pl';; esac
-
跳出循环
-
break 命令允许跳出所有循环
echo "welcome to Amusement park" echo "please inter a number between 1-4" while : doread -p "please inter your number: " numcase $num in1|2|3|4)echo "you choose 1-4 you are great";;*)echo "you choose others you are bad"break;;esac done
-
continue 命令不会跳出所有循环,仅仅跳出当前循环。
echo "welcome to Amusement park" echo "please inter a number between 1-4" while : doread -p "please inter your number: " numcase $num in1|2|3|4)echo "you choose 1-4 you are great";;*)echo "you choose others you are bad"continueecho 'game over';;esac done #运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句 echo "游戏结束" 永远不会被执行。
-