循环结构的多个控制命令对比与实际案例

1.break、continue、exit 在shell脚本中的循环应用与实例。

#!/bin/bash
for ((i=0;i<=10;i++))
do
        if [ $i -eq 5 ];then
                continue
                #break
                #exit 1
        fi
        echo $i
done
echo "OK"

sh break.sh (continue 只跳过符合条件的那次循环)
0
1
2
3
4
6
7
8
9
10
OK

sh break.sh (break 符合条件后跳出循环)
0
1
2
3
4
OK

 sh break.sh (exit 直接退出脚本,然后$?返回结果为1)
0
1
2
3
4
echo $?     
1

实例:

#!/bin/bash
RE=0
case "$1" in
up)
        for ((i=0;i<=15;i++))
        do
                if [ $i -eq 10 ];then
                        continue
                fi
        ifconfig eth4:$i 10.0.2.$i netmask 255.255.254.0 up $1
done

;;
down)
        for ((i=0;i<=16;i++))
        do
                if [ $i -eq 10 ];then
                        continue
                fi
        ifconfig eth4:$i 10.0.2.$i netmask 255.255.254.0 $1
done

;;
*)
        echo "usage:$0 {up|dowe}"
        exit 1
esac
exit $RE

 

posted @ 2017-08-31 14:33  肖咏卓  阅读(209)  评论(0编辑  收藏  举报