我有一个大问题。我的脚本在我的Debian Squeeze本地VM和Mac OX 10.8上运行良好,但在我的Debian服务器上却无法运行...我检查了内核,bash的所有版本,...在所有方面都是相同的!
我的剧本:
#!/bin/bash
# Version 1.0
ipaddr=$1
datel=$(date +"%d/%m/%Y %k:%M")
function valid_ip()
{
local ip=$ipaddr
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
valid_ip
if [ $? -eq "0" ]; then
nmap -sS -A $ipaddr --max-retries 3 -oX landiscover.xml --webxml 2>> nmap_error.log
echo "$datel Please wait during discover your network, this operation may take a while" >> landiscover.log
else
echo "$datel Please enter a valid network address : XXX.XXX.XXX./XX" >> landiscover.log
fi
if [ $? -eq "0" ]; then
echo "$datel Landiscover ran successfully !" >> landiscover.log
fi
当我使用debug选项运行此脚本时,我得到以下输出:
loterm_g@vm11:/opt$ sh -x landiscover.sh 192.168.1.0/24
+
: not found.sh: 1:
+ ipaddr=192.168.1.0/24
+ date +%d/%m/%Y %k:%M
+ datel=30/07/2013 1:54
+
: not found.sh: 1:
landiscover.sh: 8: Syntax error: "(" unexpected
任何想法 ?
最佳答案
您正在使用sh
解释器运行脚本,该解释器不是(不一定)是bash,并且不了解数组。
另外,错误消息似乎表明您的脚本中有回车符。您是否在Windows上开发了它?
关于shell - 为什么 “(”在bash脚本中是意外的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17936207/