for循环,while循环,break跳出循环,continue结束本次循环,exit直接退出脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for循环,while循环,break跳出循环,continue结束本次循环,exit直接退出脚本相关的知识,希望对你有一定的参考价值。
for循环
- 语法:for 变量名 in 条件; do …; done
- 案列1,算出1到10的数字相加等于多少并打印出过程
[[email protected] shell]# cat for1.sh #!/bin/bash sum=0 for i in `seq 1 10` do sum=$[$sum+$i] echo "$sum + $i" done echo $sum [[email protected] shell]# sh -x for1.sh + sum=0 ++ seq 1 10 + for i in ‘`seq 1 10`‘ + sum=1 + echo ‘1 + 1‘ 1 + 1 + for i in ‘`seq 1 10`‘ + sum=3 + echo ‘3 + 2‘ 3 + 2 + for i in ‘`seq 1 10`‘ + sum=6 + echo ‘6 + 3‘ 6 + 3 + for i in ‘`seq 1 10`‘ + sum=10 + echo ‘10 + 4‘ 10 + 4 + for i in ‘`seq 1 10`‘ + sum=15 + echo ‘15 + 5‘ 15 + 5 + for i in ‘`seq 1 10`‘ + sum=21 + echo ‘21 + 6‘ 21 + 6 + for i in ‘`seq 1 10`‘ + sum=28 + echo ‘28 + 7‘ 28 + 7 + for i in ‘`seq 1 10`‘ + sum=36 + echo ‘36 + 8‘ 36 + 8 + for i in ‘`seq 1 10`‘ + sum=45 + echo ‘45 + 9‘ 45 + 9 + for i in ‘`seq 1 10`‘ + sum=55 + echo ‘55 + 10‘ 55 + 10 + echo 55 55
- 文件列表循环,列出/root/shell/目录下子目录中的所有文件
[[email protected] shell]# cat for2.sh #!/bin/bash cd /root/shell/ for a in `ls /root/shell` do if [ -d $a ] then ls $a fi done [[email protected] shell]# sh -x for2.sh + cd /root/shell/ ++ ls /root/shell + for a in ‘`ls /root/shell`‘ + ‘[‘ -d 1 ‘]‘ + ls 1 1.txt 2.txt 3.txt + for a in ‘`ls /root/shell`‘ + ‘[‘ -d 1.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d 2 ‘]‘ + ls 2 a.txt b.txt c.txt + for a in ‘`ls /root/shell`‘ + ‘[‘ -d 3 ‘]‘ + ls 3 + for a in ‘`ls /root/shell`‘ + ‘[‘ -d 4 ‘]‘ + ls 4 + for a in ‘`ls /root/shell`‘ + ‘[‘ -d case.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d file1.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d file2.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d for1.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d for2.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d if1.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d if2.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d if3.sh ‘]‘ + for a in ‘`ls /root/shell`‘ + ‘[‘ -d if4.sh ‘]‘ [[email protected] shell]# sh for2.sh 1.txt 2.txt 3.txt a.txt b.txt c.txt
- for循环是以空格和回车作为分割符,所以不支持列出带有空格的文件名的文件
while循环
- 语法 while 条件; do … ; done
- 案列:当系统的负载大于10的时候就发一封邮件
[[email protected] shell]# cat while1.sh #!/bin/bash #死循环,:是一个死循环,也可以写成1或者true while : do #截取负载的值并赋给变量load load=`w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1` #如果负载大于10就报警并发送邮件,设置30秒查一次 if [ $load -gt 10 ] then /usr/lib/zabbix/alertscripts/mail.py [email protected] "load is high:$load" fi sleep 30 done [[email protected] shell]# sh -x while1.sh + : ++ head -1 ++ cut -d. -f1 ++ w ++ awk -F ‘load average: ‘ ‘{print $2}‘ + load=0 + ‘[‘ 0 -gt 10 ‘]‘ + sleep 30 ^C
- 截取负载的值
[[email protected] shell]# uptime|awk -F ‘load average: ‘ ‘{print $2}‘ 0.01, 0.03, 0.05 [[email protected] shell]# uptime|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1 0 #w|head -1也可以写成uptime [[email protected] shell]# w |head -1 01:02:36 up 58 min, 1 user, load average: 0.07, 0.04, 0.05 [[email protected] shell]# uptime 01:02:46 up 59 min, 1 user, load average: 0.06, 0.04, 0.05
- 案列2:数字输入交互
[[email protected] shell]# cat while2.sh #!/bin/bash while : do read -p "Please input a number: " n #如果你什么也没输入 if [ -z "$n" ] then echo "你需要输入一个数字" #从头开始循环 continue fi #过滤数字并赋予n1 n1=`echo $n|sed ‘s/[0-9]//g‘` #如果n1不为空,提示 if [ -n "$n1" ] then echo "你只能输入一个纯数字" continue fi #退出循环 break done echo $n [[email protected] shell]# sh while2.sh Please input a number: 你需要输入一个数字 Please input a number: a 你只能输入一个纯数字 Please input a number: 2 2
break跳出循环
- 案列:当i等于3时,跳出循环并打印aaaa
[[email protected] shell]# cat break.sh #!/bin/bash for i in `seq 1 5` do echo $i if [ $i -eq 3 ] then break fi echo $i done echo aaaa [[email protected] shell]# sh -x break.sh ++ seq 1 5 + for i in ‘`seq 1 5`‘ + echo 1 1 + ‘[‘ 1 -eq 3 ‘]‘ + echo 1 1 + for i in ‘`seq 1 5`‘ + echo 2 2 + ‘[‘ 2 -eq 3 ‘]‘ + echo 2 2 + for i in ‘`seq 1 5`‘ + echo 3 3 + ‘[‘ 3 -eq 3 ‘]‘ + break + echo aaaa aaaa
continue结束本次循环
- 忽略continue之下的代码,直接进行下一次循环
- 案列:当i不等于3的时候打印出i,当i等于3的时候退出本次循环不打印
[[email protected] shell]# cat continue.sh #!/bin/bash for i in `seq 1 5` do if [ $i -eq 3 ] then continue fi echo $i done echo aaaa [[email protected] shell]# sh -x continue.sh ++ seq 1 5 + for i in ‘`seq 1 5`‘ + ‘[‘ 1 -eq 3 ‘]‘ + echo 1 1 + for i in ‘`seq 1 5`‘ + ‘[‘ 2 -eq 3 ‘]‘ + echo 2 2 + for i in ‘`seq 1 5`‘ + ‘[‘ 3 -eq 3 ‘]‘ + continue + for i in ‘`seq 1 5`‘ + ‘[‘ 4 -eq 3 ‘]‘ + echo 4 4 + for i in ‘`seq 1 5`‘ + ‘[‘ 5 -eq 3 ‘]‘ + echo 5 5 + echo aaaa aaaa
exit直接退出脚本
- 案列:当i不等于3的时候打印出i,当i等于3的时候直接退出脚本
[[email protected] shell]# cat exit.sh #!/bin/bash for i in `seq 1 5` do if [ $i -eq 3 ] then exit fi echo $i done echo aaaa [[email protected] shell]# sh -x exit.sh ++ seq 1 5 + for i in ‘`seq 1 5`‘ + ‘[‘ 1 -eq 3 ‘]‘ + echo 1 1 + for i in ‘`seq 1 5`‘ + ‘[‘ 2 -eq 3 ‘]‘ + echo 2 2 + for i in ‘`seq 1 5`‘ + ‘[‘ 3 -eq 3 ‘]‘ + exit [[email protected] shell]# sh exit.sh 1 2
扩展
- select用法
以上是关于for循环,while循环,break跳出循环,continue结束本次循环,exit直接退出脚本的主要内容,如果未能解决你的问题,请参考以下文章
for循环 while循环 break跳出循环 continue结束本次循环 exit退出整个脚本
20.10 for循环 20.11/20.12 while循环 20.13 break跳出循环 20
java里面的一个break是跳出一层for循环,还是可以跳出几层