bash 脚本中 ping 多个 IP 地址时出错
Welcome to the IP Check Program!! Please input the ip address: 300.500
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
IP address is alive
Thank you for using the IP Check Program. \nPress 0 to exit and any other number to
continue
有人可以帮我吗?这是显示的错误。我猜为什么该程序被搞砸了。因为“ 300.500”显然不是一个有效的IP地址,但它读取为活着。请帮忙!我的代码在下面
#!/bin/bash
rs=1
while [ $rs -gt 0 ]
do
clear
#greet the user and request input
echo -n "Welcome to the IP Check Program!! Please input the ip address: "
read ip
#check user's input is IP address format
isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
isIP=$?
if [ $isIP -eq 0 ];
then
isValid=1
for i in 1 2 3 4
do
n='echo $ip | cut -d. -f$i'
if [ "$n" -gt 255 -o $n -lt 0 ];
then
echo "INPUT ERROR. IP ADDRESS DIGITS SHOULD BE BETWEEN 0 AND 255
ONLY"
isValid=0
fi
done
if [ "$isValid" -eq 1 ];
then
echo "IP address is alive"
fi
else
echo "IP address is not responding"
fi
#greet user after using program
echo -n "Thank you for using the IP Check Program. Press 0 to exit and any other number
to continue"
read rs
Welcome to the IP Check Program!! Please input the ip address: 300.500
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
IP address is alive
Thank you for using the IP Check Program. \nPress 0 to exit and any other number to
continue
could someone please help me out? this is the error displayed. i guess why the program is messed up. because "300.500" is obviously not a valid ip address but it reads it as alive. PLEASE HELP! My code is below
#!/bin/bash
rs=1
while [ $rs -gt 0 ]
do
clear
#greet the user and request input
echo -n "Welcome to the IP Check Program!! Please input the ip address: "
read ip
#check user's input is IP address format
isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
isIP=$?
if [ $isIP -eq 0 ];
then
isValid=1
for i in 1 2 3 4
do
n='echo $ip | cut -d. -f$i'
if [ "$n" -gt 255 -o $n -lt 0 ];
then
echo "INPUT ERROR. IP ADDRESS DIGITS SHOULD BE BETWEEN 0 AND 255
ONLY"
isValid=0
fi
done
if [ "$isValid" -eq 1 ];
then
echo "IP address is alive"
fi
else
echo "IP address is not responding"
fi
#greet user after using program
echo -n "Thank you for using the IP Check Program. Press 0 to exit and any other number
to continue"
read rs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@isioma-udobi,您需要改进您的脚本。它有几个基本问题必须先解决,然后才能使其发挥作用。如果你阅读了戈登告诉你的内容,应该会有很大帮助。我重新编写了您的代码,进行了一些更改并向其添加了一些教学评论。看一下,看看您的脚本是否可以取得进展:
@isioma-udobi , you need to improve your script. It has several basic problems that must be solved before you can make it work. What Gordon told you should help a lot, if you read what is pointed. And I reformated your code, made a few changes and added some didactic comments to it. Take a look, and see if you can progress with your script:
我会为此使用
nmap
而不是编写自定义Shell脚本。您可以这样扫描IP的范围:或给它在文件中提供一个主机列表:
人页。
I would use
nmap
for this instead of writing a custom shell script. You can scan ranges of IP's like this:or give it a list of hosts in a file:
more info in the man page.