我希望能够验证在bash脚本中是否有IP形式的东西,并且我在网上发现了各种代码片段......它们都有相同的结构..BASH测试错误[[]]
#!/bin/bash
valid_ip()
{
local ip=$1
echo $ip
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
ret=0 # is an IP
else
ret=1 # isn't an IP
fi
return $ret
}
# SCRIPT -------------------------------------
#clear the table
ipfw table 1 flush
ips=$(dig -f ./hostnames.txt +short)
# For each of the IPs check that it is a valid IP address
# then check that it does not exist in the ips file already
# if both checks pass append the IP to the file
for ip in $ips
do
if valid_ip $ip; then
if grep -R "$ip" "~/Dropbox/ProxyBox Stuff/dummynet/ips.txt"; then
echo "$ip already exists"
else
echo $ip >> ips.txt
fi
fi
done
# get the IP's and add them to table 1
cat ips.txt | while read line; do
ipfw table 1 add $line
done
反正我收到以下错误
./script.sh: 18: ./script.sh: [[: not found
我不明白为什么我无法完成这个测试...任何帮助,将不胜感激。
我打电话与
sudo ./script.sh
剧本,我相信使用sudo是造成这个问题的,但我需要须藤PFOR我的剧本的其他部分。
嗯,您提供的代码段适用于我。你确定这是错误的第18行吗? – 2013-03-09 23:37:39
哪个bash版本? – Kent 2013-03-09 23:38:05
你怎么称呼这个scipt? – choroba 2013-03-09 23:38:15