shell中break,continue,exit的应用

中断及退出
break,continue,exit

continue: 跳转至下一次循环
break:结束循环
exit:退出脚本

for i in {1..254}
do
      [ $i -eq 10 ];continue
      ssh 192.168.4.$i shutdown -h now
done

[root@vh01 script]# vim continue.sh
for i in {1..5}
do
      [ $i -eq 3 ]&& continue
      echo $i
done

[root@vh01 script]# vim break.sh
for i in {1..5}
do
      [ $i -eq 3 ]&& break
      echo $i
done

[root@vh01 script]# vim exit.sh
for i in {1..5}
do
      [ $i -eq 3 ]&& exit
      echo $i
done


[root@vh01 script]# vim sum.sh
#!/bin/bash
sum=0
while :
do
        read -p "请输入数字:" num
        [ $num == end ]&&break
        sum=$[sum+num]
done
echo "总和为$sum"

上一篇:Break Continue


下一篇:Go语言基础之流程控制for循环