for循环 while循环break跳出循环continue结束本次循环exit退出整个脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for循环 while循环break跳出循环continue结束本次循环exit退出整个脚本相关的知识,希望对你有一定的参考价值。

for循环

技术分享图片

案例1:写个for循环的脚本需求,我让你计算1到100所有数字的和。

技术分享图片

技术分享图片

上图有了数字之后就去做加减法

技术分享图片

[[email protected] shell]# sh -x for1.sh           ##执行查询过程
+ sum=0                  ##sum=0
++ seq 1 100            
+ for i in ‘`seq 1 100`‘        ##循环一次是1+0等于1
+ sum=1
+ for i in ‘`seq 1 100`‘       ##第二次循环是1+2等于3
+ sum=3
+ for i in ‘`seq 1 100`‘          ##第三次循环 3+3等于6  
+ sum=6
+ for i in ‘`seq 1 100`‘         ##第四次循环6+4等于10   以下以此类推
+ sum=10
+ for i in ‘`seq 1 100`‘
+ sum=15
+ for i in ‘`seq 1 100`‘
+ sum=21
+ for i in ‘`seq 1 100`‘
+ sum=28
+ for i in ‘`seq 1 100`‘
+ sum=36
+ for i in ‘`seq 1 100`‘
+ sum=45
+ for i in ‘`seq 1 100`‘
+ sum=55
+ for i in ‘`seq 1 100`‘
+ sum=66
+ for i in ‘`seq 1 100`‘
+ sum=78
+ for i in ‘`seq 1 100`‘
+ sum=91
+ for i in ‘`seq 1 100`‘
+ sum=105
+ for i in ‘`seq 1 100`‘
+ sum=120
+ for i in ‘`seq 1 100`‘
+ sum=136
+ for i in ‘`seq 1 100`‘
+ sum=153
+ for i in ‘`seq 1 100`‘
+ sum=171
+ for i in ‘`seq 1 100`‘
+ sum=190
+ for i in ‘`seq 1 100`‘
+ sum=210
+ for i in ‘`seq 1 100`‘
+ sum=231
+ for i in ‘`seq 1 100`‘
+ sum=253
+ for i in ‘`seq 1 100`‘
+ sum=276
+ for i in ‘`seq 1 100`‘
+ sum=300
+ for i in ‘`seq 1 100`‘
+ sum=325
+ for i in ‘`seq 1 100`‘
+ sum=351
+ for i in ‘`seq 1 100`‘
+ sum=378
+ for i in ‘`seq 1 100`‘
+ sum=406
+ for i in ‘`seq 1 100`‘
+ sum=435
+ for i in ‘`seq 1 100`‘
+ sum=465
+ for i in ‘`seq 1 100`‘
+ sum=496
+ for i in ‘`seq 1 100`‘
+ sum=528
+ for i in ‘`seq 1 100`‘
+ sum=561
+ for i in ‘`seq 1 100`‘
+ sum=595
+ for i in ‘`seq 1 100`‘
+ sum=630
+ for i in ‘`seq 1 100`‘
+ sum=666
+ for i in ‘`seq 1 100`‘
+ sum=703
+ for i in ‘`seq 1 100`‘
+ sum=741
+ for i in ‘`seq 1 100`‘
+ sum=780
+ for i in ‘`seq 1 100`‘
+ sum=820
+ for i in ‘`seq 1 100`‘
+ sum=861
+ for i in ‘`seq 1 100`‘
+ sum=903
+ for i in ‘`seq 1 100`‘
+ sum=946
+ for i in ‘`seq 1 100`‘
+ sum=990
+ for i in ‘`seq 1 100`‘
+ sum=1035
+ for i in ‘`seq 1 100`‘
+ sum=1081
+ for i in ‘`seq 1 100`‘
+ sum=1128
+ for i in ‘`seq 1 100`‘
+ sum=1176
+ for i in ‘`seq 1 100`‘
+ sum=1225
+ for i in ‘`seq 1 100`‘
+ sum=1275
+ for i in ‘`seq 1 100`‘
+ sum=1326
+ for i in ‘`seq 1 100`‘
+ sum=1378
+ for i in ‘`seq 1 100`‘
+ sum=1431
+ for i in ‘`seq 1 100`‘
+ sum=1485
+ for i in ‘`seq 1 100`‘
+ sum=1540
+ for i in ‘`seq 1 100`‘
+ sum=1596
+ for i in ‘`seq 1 100`‘
+ sum=1653
+ for i in ‘`seq 1 100`‘
+ sum=1711
+ for i in ‘`seq 1 100`‘
+ sum=1770
+ for i in ‘`seq 1 100`‘
+ sum=1830
+ for i in ‘`seq 1 100`‘
+ sum=1891
+ for i in ‘`seq 1 100`‘
+ sum=1953
+ for i in ‘`seq 1 100`‘
+ sum=2016
+ for i in ‘`seq 1 100`‘
+ sum=2080
+ for i in ‘`seq 1 100`‘
+ sum=2145
+ for i in ‘`seq 1 100`‘
+ sum=2211
+ for i in ‘`seq 1 100`‘
+ sum=2278
+ for i in ‘`seq 1 100`‘
+ sum=2346
+ for i in ‘`seq 1 100`‘
+ sum=2415
+ for i in ‘`seq 1 100`‘
+ sum=2485
+ for i in ‘`seq 1 100`‘
+ sum=2556
+ for i in ‘`seq 1 100`‘
+ sum=2628
+ for i in ‘`seq 1 100`‘
+ sum=2701
+ for i in ‘`seq 1 100`‘
+ sum=2775
+ for i in ‘`seq 1 100`‘
+ sum=2850
+ for i in ‘`seq 1 100`‘
+ sum=2926
+ for i in ‘`seq 1 100`‘
+ sum=3003
+ for i in ‘`seq 1 100`‘
+ sum=3081
+ for i in ‘`seq 1 100`‘
+ sum=3160
+ for i in ‘`seq 1 100`‘
+ sum=3240
+ for i in ‘`seq 1 100`‘
+ sum=3321
+ for i in ‘`seq 1 100`‘
+ sum=3403
+ for i in ‘`seq 1 100`‘
+ sum=3486
+ for i in ‘`seq 1 100`‘
+ sum=3570
+ for i in ‘`seq 1 100`‘
+ sum=3655
+ for i in ‘`seq 1 100`‘
+ sum=3741
+ for i in ‘`seq 1 100`‘
+ sum=3828
+ for i in ‘`seq 1 100`‘
+ sum=3916
+ for i in ‘`seq 1 100`‘
+ sum=4005
+ for i in ‘`seq 1 100`‘
+ sum=4095
+ for i in ‘`seq 1 100`‘
+ sum=4186
+ for i in ‘`seq 1 100`‘
+ sum=4278
+ for i in ‘`seq 1 100`‘
+ sum=4371
+ for i in ‘`seq 1 100`‘
+ sum=4465
+ for i in ‘`seq 1 100`‘
+ sum=4560
+ for i in ‘`seq 1 100`‘
+ sum=4656
+ for i in ‘`seq 1 100`‘
+ sum=4753
+ for i in ‘`seq 1 100`‘
+ sum=4851
+ for i in ‘`seq 1 100`‘
+ sum=4950
+ for i in ‘`seq 1 100`‘
+ sum=5050
+ echo 5050
5050

查看每次循环的值

技术分享图片

[[email protected] shell]# sh for1.sh
0 + 1
1
1 + 2
3
3 + 3
6
6 + 4
10
10 + 5
15
15 + 6
21
21 + 7
28
28 + 8
36
36 + 9
45
45 + 10
55
55 + 11
66
66 + 12
78
78 + 13
91
91 + 14
105
105 + 15
120
120 + 16
136
136 + 17
153
153 + 18
171
171 + 19
190
190 + 20
210
210 + 21
231
231 + 22
253
253 + 23
276
276 + 24
300
300 + 25
325
325 + 26
351
351 + 27
378
378 + 28
406
406 + 29
435
435 + 30
465
465 + 31
496
496 + 32
528
528 + 33
561
561 + 34
595
595 + 35
630
630 + 36
666
666 + 37
703
703 + 38
741
741 + 39
780
780 + 40
820
820 + 41
861
861 + 42
903
903 + 43
946
946 + 44
990
990 + 45
1035
1035 + 46
1081
1081 + 47
1128
1128 + 48
1176
1176 + 49
1225
1225 + 50
1275
1275 + 51
1326
1326 + 52
1378
1378 + 53
1431
1431 + 54
1485
1485 + 55
1540
1540 + 56
1596
1596 + 57
1653
1653 + 58
1711
1711 + 59
1770
1770 + 60
1830
1830 + 61
1891
1891 + 62
1953
1953 + 63
2016
2016 + 64
2080
2080 + 65
2145
2145 + 66
2211
2211 + 67
2278
2278 + 68
2346
2346 + 69
2415
2415 + 70
2485
2485 + 71
2556
2556 + 72
2628
2628 + 73
2701
2701 + 74
2775
2775 + 75
2850
2850 + 76
2926
2926 + 77
3003
3003 + 78
3081
3081 + 79
3160
3160 + 80
3240
3240 + 81
3321
3321 + 82
3403
3403 + 83
3486
3486 + 84
3570
3570 + 85
3655
3655 + 86
3741
3741 + 87
3828
3828 + 88
3916
3916 + 89
4005
4005 + 90
4095
4095 + 91
4186
4186 + 92
4278
4278 + 93
4371
4371 + 94
4465
4465 + 95
4560
4560 + 96
4656
4656 + 97
4753
4753 + 98
4851
4851 + 99
4950
4950 + 100
5050
5050

技术分享图片

把/etc下的文件做下编译,把所有目录里边的文件全部ls列出来

技术分享图片

while循环

技术分享图片

技术分享图片

需求每隔半分钟检查下系统的负载,当系统的负载大于10的时候就发一封邮件,这是个监控脚本

例子如下

#!/bin/bash
while :                  ##冒号表示死循环
do
       load=`w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1`    ##这条的命令会把系统的负载复制到load这个变量里
             if [ $load -gt 10 ]           ##逻辑判断当load大于10的时候
             then           ##然后怎么办
                  /usr/local/sbin/mail.py [email protected] "load high" "$load"      ##发邮件给 [email protected],第二个参数主题load high 第三个主题$load
              fi
                sleep 30       ##间断的时间30秒查一次
done

技术分享图片
技术分享图片

while循环案例2,死循环脚本,在循环当中需要你去人为的交互输入一个数字,检测数字是不是你想要的东西

#!/bin/bash
while :
do
    read -p "Please input a number: " n             ##
    if [ -z "$n" ]
    then
        echo "you need input sth."
        continue         ##继续执行大循环
    fi
    n1=`echo $n|sed ‘s/[0-9]//g‘`
    if [ -n "$n1" ]
    then
        echo "you just only input numbers."
        continue
    fi
    break
done
echo $n

技术分享图片

技术分享图片

break跳出循环

技术分享图片

#!/bin/bash
for i in `seq 1 5`
do
? ? echo $i     ##把i的值输出
? ? if [ $i -eq 3 ]   ##然后做个判断,当i等于3的时候
? ? then
? ?? ???break    ##跳出循环
? ?fi
    echo $i
done
echo aaaa

技术分享图片
技术分享图片

continue结束本次循环

技术分享图片

exit退出整个脚本

技术分享图片

以上是关于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循环,还是可以跳出几层

70.for循环while循环break跳出循环continue结束本次循环exit

光计划2

for循环while循环break跳出循环continue结束本次循环exit退出整个脚本