您的位置:首页 > 运维架构 > Shell

Shell脚本判断IP是否合法性(多种方法)

2017-08-17 13:58 507 查看
运维角度来说,写shell脚本经常会遇到判断输入的值是否合法,比如IP、邮件地址等。那么,根据自身写脚本中总结的判断IP合法性脚本分享给网友,遇到时能有所参考。思路:IP由四位数字组成,以点分割,每个字段不能大于255,必须符合这种格式方法1:
方法2:
加个循环,如果错误则重新输入,直到正确:
#!/bin/shcheck_ip() {local IP=$1VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; thenif [ $VALID_CHECK == "yes" ]; thenecho "IP $IP available!"return 0elseecho "IP $IP not available!"return 1fielseecho "IP format error!"return 1fi}break_con() {if [ $1 -eq 0 ];thenbreakelsecontinuefi}while :doif read -p "Do you want to install deploy-platform now [Y/N]:"thencase $REPLY inY|y) #Ywhile :doread -p "please input the repo_ip of deploy-platform eg[10.0.0.10]:"echo "repo_ip=$REPLY"repo_ip=$REPLYcheck_ip $repo_ipbreak_con $?doneecho -e "\n start install deploy-platform now \n detail in /var/log/puppet_installdeploy.log \n"sed -i -e 's/$repo_ip =.*$/$repo_ip = '$REPLY'/' /var/tmp/openstackha/deploy/manifests/init.pppuppet apply -l /root/puppet_installdeploy.log --modulepath /var/tmp/openstackha/puppet/modules/ /var/tmp/openstackha/deploy/manifests/init.pp -dbreak;;N|n) #Necho -e "\n Not install deploy-platform! \n"break;;*) #input error repeatecho -e "\n input parameter error !! \n"continueesacfidone
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: