shell - 为什么 “(”在bash脚本中是意外的?

标签 shell debian syntax-error

我有一个大问题。我的脚本在我的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/

相关文章:

linux - 如何以编程方式(脚本、 Node 或 c)将网络接口(interface)设置为静态或 DHCP?

debian - debian stretch 上的 google-fluentd 错误(Google Logging Agent)

花括号语法错误后的javascript点? : {num:1}. 编号

java - Java 时区枚举中的 Eclipse 变量错误

Python 运行时错误(SyntaxErrorException): default value must be specified here

android -/proc/wakelocks 的列是什么意思?

java - 将 Mac Dock 图标上的放置事件传递给 shell 脚本及其子进程

python - UWSGI 无法在 Debian 9 (pip) 上安装

linux - 递归循环遍历目录并打印重要文件位置的 Shell 脚本

python - 查找不包括测试用例的 Python 代码总行数