#判断IP是否符合标准规则function judge_ip () { #这里local $1 error, with 2 >/dev/null Shield off error, no effect on the output results local $1 2>/dev/null TMP_TXT=/tmp/iptmp.txt echo $1 > ${tmp_txt} ipaddr= ' grep -Eo ' ([0-9]{1,3}\.) {3} [0-9] {1,3} ' ${tmp_txt} ' #判断有没有符合 ip of the ***.***.***.*** rule if [ ! -z "${ipaddr}" ];then local j=0; # Loop to detect if the values before each point meet the requirements for  ((i=1;i<=4;i++)) do local ip_num= ' echo "${IPADDR}" |awk -F. "{print $" $i "}" ' #判断IP_NUM是否在0与255之间 if [ "${ip_num}" -ge 0 -a "${ip_num}" -le 255 ];then ((j + +)); else return 1 fi done # The value of J to determine whether to continue matching rules, loop four times, if all are correct j=4. if [ "$j" -eq 4 ];then # Verify that the IP address you want to enter is &NBSP;&NBSP;&Nbsp; read -n 1 -p "The IP you entered is ${ipaddr}, Confirm input: y|y; re-enter: R|r: " ok case $ {ok} in y|y) return 0;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;R|R) return 1;; *) return 1;; esac else Return 1 fi else &NBSP;RETURN&NBSP;1&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FI}
Call Method:
Read-p "To set the IP example for" 192.168.1.1 ", enter:" Ipaddrsjudge_ip "${ipaddrs}"; I= ' echo $? ' #循环直到输入正确的IP为止until ["$i"-eq 0];d Oecho -E "\033[31m you entered the wrong Ip:${ipaddrs} ====>>>>\033[0m" Read-p "re-enter the IP, example" 192.168.1.1 ", enter:" Ipaddrsjudge_ip " ${ipaddrs} "; I= ' echo $? ' Done
This article is from the "Mountain Road 18 Bend" blog, please be sure to keep this source http://kongzi68.blog.51cto.com/1432619/1644013
The shell script function determines whether the IP address entered conforms to the rule