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退出整个脚本

原文地址:http://blog.51cto.com/8043410/2176619

时间: 2024-11-05 22:32:09

for循环、 while循环、break跳出循环、continue结束本次循环、exit退出整个脚本的相关文章

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

20.10 for循环1到100相加的值执行结果 打印出1-100的数字 if [ -d $a ]thenls -d $afi它相当于 [ -d $a ] && ls $a 上图创建的3空格4.txt是一个文件,查在for语句循环的时候将它拆分成3与4.txt两个文件了,说明for循环是以空格作为分隔符的,这个问题要注意20.11 20.12 while循环需求每隔半分钟检查一下系统的负载,当负载大于10的时候就发一封邮件,隔30 秒执行一次这是一个死循环脚本30秒执行一次这个脚本是让用户

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

一.for循环需求:计算1-100所有数字的和[[email protected] shell]# vi for1.sh#!/bin/bashfor i in seq 1 100doecho $i //先把100个数字打印出来done[[email protected] shell]# sh for1.sh //执行脚本打印出100个数字 继续修改脚本:[[email protected] shell]# vi for1.sh#!/bin/bashsum=0for i in seq 1 100d

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

原文地址:https://www.cnblogs.com/xiaobo-Linux/p/8920368.html

for与while循环、break跳出循环、continue结束本次循环、exit退出脚本

for循环 while循环 break跳出循环 continue结束本次循环 exit退出脚本 原文地址:http://blog.51cto.com/13515599/2106958

shell脚本 for循环、break跳出循环、continue结束本次循环

20.10 for循环 语法:for 变量名 in 条件; do ...; done ;案例1 [[email protected] shell]# cat for.sh #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] done echo $sum #输出的结果 [[email protected] shell]# sh for.sh 5050 文件列表循环 [[email protected] shell]# cat for2.

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

for 循环 当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值.命令可为任何有效的shell命令和语句. 语法:for 变量名 in 条件: do...;done 案例1 #!/bin/bashsum=0 // 给变量sum赋值for i in seq 1 100 // 给i赋值,从1到100dosum=$[ $sum + $i ]// 累加doneecho $sum// 输出结果为1到100的和 案例2 #!/bin/bashcd /etc/ // 切换到/etc

Shell脚本(for循环,while循环,break跳出循环,continue结束本次循环)

for循环 语法:for 变量名 in 条件 : do done: 案例一: 计算1-100所有数字的和. 脚本: #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] done echo $sum 结果: [[email protected] ~]# sh 1-100.sh 5050 案例二: 列出/etc/sysconfig下所有子目录,并且使用ls -d命令查看. 脚本: #/bin/bash cd /etc/sysconfig

六十九、for循环、while循环、break跳出循环、continue结束本次循环、exit退出整

六十九.for循环.while循环.break跳出循环.continue结束本次循环.exit退出整个脚本 一.for循环 语法:for 变量名 in 条件; do -; done 案例1 # vim for1.sh #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i]     //核心语句 echo $i done echo $sum //最后再echo $sum,不能在for循环里echo,不然每次的sum都打印出来了.让它每次都打

20.10 for循环 20.11/20.12 while循环 20.13 break跳出循环 20.14 continue结束本次循环 20.15 exit退出整个脚本

20.10 for循环 ?语法:for 变量名 in 条件; do -; done ? 案例1 1+2+3..+100的和 #!/bin/bash sum=0 for i in `seq 1 100` // seq 1到100个数字 do sum=$[$sum+$i] echo $i done echo $sum sum 第一次作为变量的时候,是0:当进入for循环里面的时候,每运算一次,sum变量就会改变一次,直至$i 结束:最后输出结果 $sum ? 案例2 文件列表循环 #!/bin/ba

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