您的位置:首页 > 运维架构 > Shell

shell练习五

2015-08-29 00:00 375 查看
摘要: shell练习五--检查输入的ip是否正确

写一个shell检查我们输入的IP是否属于正确的格式

#! /bin/bash

checkip() {
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}'`

for n in $a $b $c $d; do
if [ $n -ge 255 ] || [ $n -le 0 ]; then
echo "the number of the IP should less than 255 and greate than 0"
return 2
fi
done
else
echo "The IP you input is something wrong, the format is like 192.168.100.1"
return 1
fi
}
rs=1
while [ $rs -gt 0 ]; do
read -p  "Please input the ip:" ip
checkip $ip
rs=`echo $?`
done
echo "The IP is right!"

[root@localhost shell]# sh check.sh
Please input the ip:192.168.1.1
The IP is right!
[root@localhost shell]# sh check.sh
Please input the ip:1234.123.12.1
The IP you input is something wrong, the format is like 192.168.100.1
Please input the ip:192.168.1.1
The IP is right!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: