我希望能够验证某些东西是 bash 脚本中的 IP 形式,并且我在网上找到了各种代码......它们都具有大致相同的结构..
#!/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 会导致问题,但我的脚本的其他部分需要 sudo p。