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