bash脚本编程while&until

bash脚本编程while&until

while

while  CONDITION; do
    循环体
    循环控制变量修正表达式
done

进入条件:CONDITION测试为”真“
退出条件:CONDITION测试为”假

until

until  CONDITION; do
    循环体
    循环控制变量修正表达式
done

进入条件:CONDITION测试为”假“
退出条件:CONDITION测试为”真“    

实例:

  • 使用until和while分别实现192.168.0.0/24 网段内,地址是否能够ping通,如ping通则输出"success!",若ping不通则输出"fail!"

wile循环

#!/bin/bash
#by eighteenxu 20180420

declare -i i=1
while [ $i -le 255 ];do
        ping -c 1 -w 1 192.168.0.$i &>/dev/null
        if [ $? -eq 0 ];then
                echo "ping 192.168.0.$i sucess!"
        else
                echo "ping 192.168.0.$i fail!"
        fi
        let i++ 
done

until循环

    #!/bin/bash
    #by eighteenxu 20180420

    declare -i i=1
    while [ $i -gt 255 ];do
            ping -c 1 -w 1 192.168.0.$i &>/dev/null
            if [ $? -eq 0 ];then
                    echo "ping 192.168.0.$i sucess!"
            else
                    echo "ping 192.168.0.$i fail!"
            fi
            let i++ 
    done

猜你喜欢

转载自blog.csdn.net/eighteenxu/article/details/80021698