Linux Shell中编写判断IP地址有效性脚本碰到了问题
首先看一下下面写好的脚本:#!/bin/bash-xvalid_ip=0while[$valid_ip]doecho-n"PleaseinputIPaddress:"re...
首先看一下下面写好的脚本:
#!/bin/bash -x
valid_ip=0
while [ $valid_ip ]
do
echo -n "Please input IP address: "
read ip
chkip=(`echo $ip|awk -F '.' '{print $1}'` `echo $ip|awk -F '.' '{print $2}'` `echo $ip|awk -F '.' '{print $3}'` `echo $ip|awk -F '.' '{print $4}'`)
if [ -z $ip ]; then
echo "Sorry, Please input an IP address."
elif [ -z ${chkip[0]} ] || [ -z ${chkip[1]} ] || [ -z ${chkip[2]} ] || [ -z ${chkip[3]} ]; then
echo "Sorry, Please input a valid IP address. (1)"
elif [ ${chkip[0]}>=255 ] || [ ${chkip[0]}<0 ] || [ ${chkip[1]}>255 ] || [ ${chkip[1]}<0 ] || [ ${chkip[2]}>255 ] || [ ${chkip[2]}<0 ] || [ ${chkip[3]}>=255 ] || [ ${chkip[3]}<=0 ]; then
echo "Sorry, Please input a valid IP address. (2)"
else
valid_ip=1
fi
done
echo ${chkip[0]}
echo ${chkip[1]}
echo ${chkip[2]}
echo ${chkip[3]}
主要是
elif [ ${chkip[0]}>=255 ] || [ ${chkip[0]}<=0 ] || [ ${chkip[1]}>255 ] || [ ${chkip[1]}<0 ] || [ ${chkip[2]}>255 ] || [ ${chkip[2]}<0 ] || [ ${chkip[3]}>=255 ] || [ ${chkip[3]}<=0 ]; then
这部分条件不管我输入的是什么,始终是为‘真’,根本执行不了else下面的代码!请问我这代码是哪里错了,已经研究了很久了,希望各位热心的网友能帮忙分析下,谢谢了! 展开
#!/bin/bash -x
valid_ip=0
while [ $valid_ip ]
do
echo -n "Please input IP address: "
read ip
chkip=(`echo $ip|awk -F '.' '{print $1}'` `echo $ip|awk -F '.' '{print $2}'` `echo $ip|awk -F '.' '{print $3}'` `echo $ip|awk -F '.' '{print $4}'`)
if [ -z $ip ]; then
echo "Sorry, Please input an IP address."
elif [ -z ${chkip[0]} ] || [ -z ${chkip[1]} ] || [ -z ${chkip[2]} ] || [ -z ${chkip[3]} ]; then
echo "Sorry, Please input a valid IP address. (1)"
elif [ ${chkip[0]}>=255 ] || [ ${chkip[0]}<0 ] || [ ${chkip[1]}>255 ] || [ ${chkip[1]}<0 ] || [ ${chkip[2]}>255 ] || [ ${chkip[2]}<0 ] || [ ${chkip[3]}>=255 ] || [ ${chkip[3]}<=0 ]; then
echo "Sorry, Please input a valid IP address. (2)"
else
valid_ip=1
fi
done
echo ${chkip[0]}
echo ${chkip[1]}
echo ${chkip[2]}
echo ${chkip[3]}
主要是
elif [ ${chkip[0]}>=255 ] || [ ${chkip[0]}<=0 ] || [ ${chkip[1]}>255 ] || [ ${chkip[1]}<0 ] || [ ${chkip[2]}>255 ] || [ ${chkip[2]}<0 ] || [ ${chkip[3]}>=255 ] || [ ${chkip[3]}<=0 ]; then
这部分条件不管我输入的是什么,始终是为‘真’,根本执行不了else下面的代码!请问我这代码是哪里错了,已经研究了很久了,希望各位热心的网友能帮忙分析下,谢谢了! 展开
展开全部
建议你先把这一堆逻辑判断分开, 或者加一些调试用的echo回显 很快就能判断出问题的
哎 虽然都是写脚本的, 可看到这个还是懒得去细看了 笨人的代码风格就是不讲究技巧 只讲究清楚
哎 虽然都是写脚本的, 可看到这个还是懒得去细看了 笨人的代码风格就是不讲究技巧 只讲究清楚
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
快又稳
2024-10-28 广告
2024-10-28 广告
在Linux环境下配置基于域名的虚拟主机,需安装Apache或Nginx等Web服务器,并编辑配置文件。以Apache为例,需创建虚拟主机配置文件,指定域名、文档根目录等,然后启用该配置文件并重启Apache服务。同样,Nginx也需在相应...
点击进入详情页
本回答由快又稳提供
展开全部
#! /bin/bash
function checkip {
for var in `echo $1 | awk -F. '{print $1, $2, $3, $4}'`
do
if [ $var -ge 1 -a $var -le 255 ]
then
continue
else
return 1
fi
done
}
echo -n 'Give me a IP address to check:'
read ip
checkip $ip
case $? in
0)
echo "Your input $ip is a valid IP address.";;
1)
echo "Your input $ip is invalid.";;
esac
function checkip {
for var in `echo $1 | awk -F. '{print $1, $2, $3, $4}'`
do
if [ $var -ge 1 -a $var -le 255 ]
then
continue
else
return 1
fi
done
}
echo -n 'Give me a IP address to check:'
read ip
checkip $ip
case $? in
0)
echo "Your input $ip is a valid IP address.";;
1)
echo "Your input $ip is invalid.";;
esac
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询