The shell script function determines whether the IP address entered conforms to the rule

Source: Internet
Author: User

#判断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&nbsp ((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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.