判斷輸入是否是IP

[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
        # 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}' `
        # 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
 
只是一個簡陋的判斷。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章