标签:shell脚本 检查ip
编写shell脚本,判断输入的IP是否正确(IP的规则是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255)。
#!/bin/bash
check_ip(){
if echo $1 | egrep -q "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$";then
a=`echo $1|awk -F. ‘{print $1}‘`
b=`echo $1|awk -F. ‘{print $2}‘`
c=`echo $1|awk -F. ‘{print $3}‘`
d=`echo $1|awk -F. ‘{print $4}‘`
if [ $a -le 1 -o $a -ge 255 ] || [ $b -le 0 -o $b -ge 255 ] || [ $c -le 0 -o $c -ge 255 ] || [ $d -le 0 -o $d -ge 255 ] ;then
echo "IP的规则是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255 "
return 1
fi
else
echo "please enter an IP like "192.168.16.1""
return 2
fi
}
stat=1
while [ $stat -ne 0 ];do
read -p "please enter an IP:" ip
check_ip $ip
stat=`echo $?`
done
echo "The ip $ip is right!"
标签:shell脚本 检查ip
原文地址:http://onelinux.blog.51cto.com/2179673/1664142