Shell编程9_while循环

while循环


格式:

while 条件 
do 
done 

【练习1】无限循环

执行脚本后跟happy关键字段,条件满足时输出内容

 #!/bin/bash
 while [ "$1" = "happy" ]
 do
         echo this\'s $1 day
 done

这里写图片描述

【练习2】break跳出当前循环

break是指跳出循环,可以执行循环外的语句

#!/bin/bash
 for i in {1..10}
 do
         if [ "$i" == "7" ]
         then
                 echo "this is break"
                 break
         fi
         echo $i
 done
 echo end

这里写图片描述

【练习3】exit结束

exit往往意味着整个脚本的结束
exit是指跳出整个脚本

#!/bin/bash
 for i in {1..10}
 do
         if [ "$i" == "7" ]
         then
                 echo "this is exit"
                 exit
         fi
         echo $i
 done
 echo end

这里写图片描述

【练习4】continue 结束当前,继续后续

continue 结束当前命令继续执行其他动作

#!/bin/bash
 for i in {1..10}
 do
         if [ "$i" == "7" ]
         then
                 echo "this is continue next:"
                 continue
         fi
         echo $i
 done
 echo end

这里写图片描述

猜你喜欢

转载自blog.csdn.net/zwhzwh0228/article/details/80881736