SupershunK
SupershunK

Reputation: 21

Why "(" is unexpected in bash script?

I have a big problem. My script works well on my Debian Squeeze local VM and Mac OX 10.8 but not on my Debian server ... I checked all the versions of kernel, bash, ... It's the same on all !

My script :

#!/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

When I run this script with debug option I have this output :

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

Any idea ?

Upvotes: 2

Views: 227

Answers (1)

glenn jackman
glenn jackman

Reputation: 246807

You're running the script using the sh interpreter which is not (necessarily) bash, and does not know about arrays.

Also, the error message seem to indicate you have carriage returns in your script. Did you develop it on Windows?

Upvotes: 1

Related Questions