Shell 为什么;(“在bash脚本中是意外的吗?”?
我有一个大问题。我的脚本在我的Debian本地虚拟机和Mac OX 10.8上运行良好,但在我的Debian服务器上运行不好…我检查了内核、bash的所有版本…所有版本都是一样的 我的剧本:Shell 为什么;(“在bash脚本中是意外的吗?”?,shell,debian,syntax-error,Shell,Debian,Syntax Error,我有一个大问题。我的脚本在我的Debian本地虚拟机和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]
#!/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
当我使用调试选项运行此脚本时,我有以下输出:
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上开发的吗?您是在使用CRLF作为行结尾而不是Unix LF的机器上编辑脚本的吗?我认为CR字符可能是这些
:未找到的错误的原因,可能还有语法错误。ip=($ip)
?那不应该是ip=$(ip)
?@PaulTomblin不,他正在把它变成一个数组。$(ip)
将尝试运行ip
命令并分配其输出。ip
是我系统上的一个命令。这可能是问题吗?@PaulTomblin否。Shell变量和命令名不会相互冲突。我敢打赌,问题在于他的脚本中的CR字符。