循环脚本简单练习

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

使用while实现:
思路:定义数值如果少于255则进入循环,判断完当前IP后,在循环体中把数值自增1,一直到255跳出。

#!/bin/bash
#
declare -i ip=1

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

使用until实现

思路:定义数值如果不是大于或等于255则进入循环,执行完循环体后,自增1,一直到255

#/bin/bash
#

declare -i ip=1

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

你可能感兴趣的:(循环脚本简单练习)