for循环及while循环

常见的循环有:for循环和while循环

1.for循环

for循环的基本格式如下:

for 变量名 in 循环的条件;do
    command
done

“循环的条件”可以是一组字符串或者一组数字,也可以是一条命令的执行结果。

这个脚本是计算0-100的和;

[root@yuioplv sbin]# vim for1.sh 
#!/bin/bash
sum=0   #sum初始值为0
for i in `seq 1 100`   #定义循环的区间
do
   sum=$[$sum+$i]   #每循环一次,将sum+i的值再赋给sum
done
echo $sum
[root@yuioplv sbin]# sh -x for1.sh 
+ sum=0
++ seq 1 100
+ for i in '`seq 1 100`'
+ sum=1
+ for i in '`seq 1 100`'
+ sum=3
+ for i in '`seq 1 100`'
+ sum=6
+ for i in '`seq 1 100`'
+ 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

这个脚本是列出/usr/local/sbin目录下.sh结尾的文件;

[root@yuioplv sbin]# vim for2.sh 
#! /bin/bash
cd /usr/local/sbin
for a in `ls ./*.sh`
do
   [ -f $a ] && ls $a
done
[root@yuioplv sbin]# sh for2.sh 
./for1.sh
./for2.sh

2.while循环

while循环的基本格式如下:

while 条件;do
    command
done

这个脚本是判断系统负载大于10时就通过/usr/loacl/sbin/mail.py脚本来发送告警邮件;

[root@yuioplv sbin]# vim while1.sh 
#!/bin/bash
while true
do
   load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`   #将1分钟内系统的平均负载值赋值给load
   if [ $load -gt 10 ]
   then
	/usr/loacl/sbin/mail.py "load high" "$load"
   fi
   sleep 30   #延时30s
done
[root@yuioplv sbin]# sh -x while1.sh 
+ true
++ head -1
++ awk -F 'load average: ' '{print $2}'
++ cut -d. -f1
++ w
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30

这个脚本是判断用户的内容,若是数字,则将输入的数字打印出来;

[root@yuioplv sbin]# vim while2.sh 
#!/bin/bash
while :
do
    read -p "请输入一个数字: " n
    if [ -z "$n" ]   #输入内容为空,则继续循环
    then
        echo "请先输入内容."
        continue
    fi
    n1=`echo $n|sed 's/[0-9]//g'`   #将输入内容的数字清空,赋值给n1
    if [ -n "$n1" ]   #判断n1不为空,继续循环
    then
        echo "你只能输入一个纯数字."
        continue
    fi
    break   #输入数字,跳出循环
done
echo $n

[root@yuioplv sbin]# sh while2.sh 
请输入一个数字: 
请先输入内容.
请输入一个数字: df
你只能输入一个纯数字.
请输入一个数字: 33
33

break:跳出循环

[root@yuioplv sbin]# vim break.sh 
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i -eq 3 ]   
    then
	break   #当i等于3时,跳出循环
    fi
    echo $i
done
echo aaa
[root@yuioplv sbin]# 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 aaa
aaa

continue:结束本次循环

[root@yuioplv sbin]# vim continue.sh 
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i -eq 3 ]   
    then
	continue   #当i等于3时,结束本次循环
    fi
    echo $i
done
echo aaa
[root@yuioplv sbin]# sh continue.sh 
1
1
2
2
3
4
4
5
5
aaa

exit:直接退出脚本

[root@yuioplv sbin]# vim exit.sh 
#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i -eq 3 ]
    then
	exit   #当i等于3时,退出脚本
    fi
    echo $i
done
echo aaa
[root@yuioplv sbin]# sh exit.sh 
1
1
2
2
3


展开阅读全文

没有更多推荐了,返回首页