阅读量:0
LInux:循环语句
if-else语句
if-else语句
if-else 语句语法格式
if [ $a -gt $b ]; then echo "a>b" else echo "a<=b" fi if [ $a -gt $b ]; then echo "a>b" echo "a!=b" echo "true" else echo "a<=b" fi if ((a>b)); then echo "true" echo "a>b" else echo "a<=b" fi
if else语法格式
if [ $a -gt $b ]; then echo "a>b" elif [$a -eq $b] then echo "a=b" else echo "a<b" fi
for 循环
while 语句
无限循环
while True; do echo "ok" done for ((::))
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 : do read -p "please inter your number: " num case $num in 1|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 : do read -p "please inter your number: " num case $num in 1|2|3|4) echo "you choose 1-4 you are great" ;; *) echo "you choose others you are bad" continue echo 'game over' ;; esac done #运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句 echo "游戏结束" 永远不会被执行。