[root@localhost tmp]# cat test.sh
#!/bin/bash
##get & test if input is a IP
touch .tmp_ip_test_file_jkj22s4wfdg
while [ -e .tmp_ip_test_file_jkj22s4wfdg ]
do
# get input ip
read -p "please input local ip:" IP
while [ -e .tmp_ip_test_file_jkj22s4wfdg ]
do
# get input ip
read -p "please input local ip:" IP
# 0 is a IP ,1 is not a IP
JIEGUO=` echo $IP | awk -F'.' '{if( ($1<256 && $1>0) && ($2<256 && $2>=0) && ($3<256 && $3 >=0) && ($4<256 && $4>=0) && NF==4) print 0;else print 1}' `
JIEGUO=` echo $IP | awk -F'.' '{if( ($1<256 && $1>0) && ($2<256 && $2>=0) && ($3<256 && $3 >=0) && ($4<256 && $4>=0) && NF==4) print 0;else print 1}' `
# test if is a IP
if [ $JIEGUO = 0 ]
then
echo $IP
rm -f .tmp_ip_test_file_jkj22s4wfdg
else
echo "The input is a bad IP ! Try again!"
fi
done
if [ $JIEGUO = 0 ]
then
echo $IP
rm -f .tmp_ip_test_file_jkj22s4wfdg
else
echo "The input is a bad IP ! Try again!"
fi
done
只是一个简陋的判断。
转载于:https://blog.51cto.com/colinzhouyj/630933