【发布时间】:2013-02-07 12:36:36
【问题描述】:
我当前的脚本:
#!/usr/local/bin/bash
echo -n "Enter VPS IP address:"
read userinput
lookupip="vps $userinput"
if [[ $userinput -lt 80.* || $userinput -gt 255.* ]] #checks input is in the range
then
echo "Input outside acceptable range."
else
#The grep removes all from VPS tool output except primary IP address
$lookupip | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | sed '1 ! d' | xargs ping -oc 1000 -Q
fi
最低 IP 地址范围在 80.X.X.X 范围内,我试过使用:
8*
80*
80...*
但它总是出错:
line 10: [[: 80.X.X.X: syntax error in expression (error token is ".X.X.X")
定义小于 (lt) 和 gt (大于) 的 IP 地址范围的最佳方法是什么?
【问题讨论】:
-
不要在变量中隐藏你正在运行的命令。它使您的代码可读性降低,并且不会像您认为的那样经常工作。只需输入
vps $userinput | grep ... -
好的,这是有道理的,但是我如何在 if 循环中巧妙地调用它?
标签: bash loops if-statement ip-address