11. break跳出循环

#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i == 3 ]
    then
        break
    fi
    echo $i
done
echo aaaaaaa
[root@feature1 ~]# sh break.sh
1
1
2
2
3
aaaaaaa
[root@feature1 ~]# sh -x break.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 == 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 == 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 == 3 ']'
+ break
+ echo aaaaaaa
aaaaaaa

12.continue结束本次循环

忽略continue之下的代码,直接进行下一次循环

#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i == 3 ]
    then
	continue
    fi
    echo $i
done
echo $i
[root@feature1 ~]# sh continue.sh
1
1
2
2
3
4
4
5
5

[root@feature1 ~]# cat break2.sh
#!/bin/bash
for i in `seq 1 5`
do
    echo "i=$i"
    for j in `seq 1 5`
    do
        echo "j=$j"
        if (($j==$i))
        then
             break
        fi
        echo "j=$j"
    done
    echo "i=$i"
done

[root@feature1 ~]# sh -x break2.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo i=1
i=1
++ seq 1 5
+ for j in '`seq 1 5`'
+ echo j=1
j=1
+ (( 1==1 ))
+ break
+ echo i=1
i=1
+ for i in '`seq 1 5`'
+ echo i=2
i=2
++ seq 1 5
+ for j in '`seq 1 5`'
+ echo j=1
j=1
+ (( 1==2 ))
+ echo j=1
j=1
+ for j in '`seq 1 5`'
+ echo j=2
j=2
+ (( 2==2 ))
+ break
+ echo i=2
i=2
+ for i in '`seq 1 5`'
+ echo i=3
i=3
++ seq 1 5
+ for j in '`seq 1 5`'
+ echo j=1
j=1
+ (( 1==3 ))
+ echo j=1
j=1
+ for j in '`seq 1 5`'
+ echo j=2
j=2
+ (( 2==3 ))
+ echo j=2
j=2
+ for j in '`seq 1 5`'
+ echo j=3
j=3
+ (( 3==3 ))
+ break
+ echo i=3
i=3
+ for i in '`seq 1 5`'
+ echo i=4
i=4
++ seq 1 5
+ for j in '`seq 1 5`'
+ echo j=1
j=1
+ (( 1==4 ))
+ echo j=1
j=1
+ for j in '`seq 1 5`'
+ echo j=2
j=2
+ (( 2==4 ))
+ echo j=2
j=2
+ for j in '`seq 1 5`'
+ echo j=3
j=3
+ (( 3==4 ))
+ echo j=3
j=3
+ for j in '`seq 1 5`'
+ echo j=4
j=4
+ (( 4==4 ))
+ break
+ echo i=4
i=4
+ for i in '`seq 1 5`'
+ echo i=5
i=5
++ seq 1 5
+ for j in '`seq 1 5`'
+ echo j=1
j=1
+ (( 1==5 ))
+ echo j=1
j=1
+ for j in '`seq 1 5`'
+ echo j=2
j=2
+ (( 2==5 ))
+ echo j=2
j=2
+ for j in '`seq 1 5`'
+ echo j=3
j=3
+ (( 3==5 ))
+ echo j=3
j=3
+ for j in '`seq 1 5`'
+ echo j=4
j=4
+ (( 4==5 ))
+ echo j=4
j=4
+ for j in '`seq 1 5`'
+ echo j=5
j=5
+ (( 5==5 ))
+ break
+ echo i=5
i=5

13.exit直接退出脚本

#!/bin/bash
for i in `seq 1 5`
do
    echo $i
    if [ $i == 3 ]
    then
	exit
    fi
    echo $i
done
echo aaaaaaa

[root@feature1 ~]# sh -x exit.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 == 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 == 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 == 3 ']'
+ exit

[root@feature1 ~]# cat test |while read line ; do echo $line ; echo "######"; done
root:x:0:0:root:/root:/bin/bash
######
bin:x:1:1:bin:/bin:/sbin/nologin
######
daemon:x:2:2:daemon:/sbin:/sbin/nologin
######
adm:x:3:4:adm:/var/adm:/sbin/nologin
######
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
######
sync:x:5:0:sync:/sbin:/bin/sync
######
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
######

03-12 08:30