1. test]# cat check_ip.sh
    2. #!/bin/bash
    3. function check_ip() {
    4. IP=$1
    5. VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}') # 检查每个数字是否小于等于255
    6. if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$">/dev/null; then # 检查格式是否为数字组成的三位数并且中间是点
    7. if [ ${VALID_CHECK:-no} == "yes" ]; then
    8. echo "IP $IP available."
    9. else
    10. echo "IP $IP not available!"
    11. fi
    12. else
    13. echo "IP $IP format error!"
    14. fi }
    15. # Example
    16. check_ip 192.168.1.1
    17. check_ip 256.1.1.1
    18. check_ip 2444.1.1.1
    1. test]# sh check_ip.sh
    2. IP 192.168.1.1 available.
    3. IP 256.1.1.1 not available!
    4. IP 2444.1.1.1 format error!
    5. test]#