while语句--shell

7 篇文章 0 订阅

while 条件
do
done
实验:当脚本后第一串字符为happy时输出this is happy day;事实上,输入happy时while条件永远都会满足,就是说这时无限循环的

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

执行结果

this is happy day
......
......
......
(无限循环)

break跳出当前循环

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

脚本执行结果,最后echo end会被执行,break只是终止do内的当前动作

[root@server210 mnt]# sh for4.sh
1
2
3
4
5
6
this's break test
end

exit往往意味着整个脚本的结束

 09 #!/bin/bash
 10 for i in {1..10}
 11 do
 12         if [ "$i" == "7" ]
 13         then
 14                 echo this\'s break test
 15                 exit
 16         fi
 17         echo $i
 18 done
 19 echo end

执行结果,最后echo end不会被执行

[root@server210 mnt]# sh for4.sh
1
2
3
4
5
6
this's break test

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

 09 #!/bin/bash
 10 for i in {1..10}
 11 do
 12         if [ "$i" == "7" ]
 13         then
 14                 echo this\'s break test
 15                 continue
 16         fi
 17         echo $i
 18 done
 19 echo end

脚本执行结果

[root@server210 mnt]# sh for4.sh
1
2
3
4
5
6
this's break test
8
9
10
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值