while语句--shell

本文详细解析了Shell脚本中的循环控制结构,包括while无限循环的实现方式、break与continue语句的区别及应用实例。通过具体示例展示了如何使用这些控制语句来精确控制脚本流程。

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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值