2

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 ?

5
  • Did you edit the script on a machine that uses CRLF for line endings, instead of Unix LF? I think CR characters are the likely reason for those : not found errors, and probably also the syntax error. Commented Jul 30, 2013 at 0:03
  • ip=($ip)? Shouldn't that be ip=$(ip)? Commented Jul 30, 2013 at 0:03
  • @PaulTomblin No, he's turning it into an array. $(ip) would try to run the ip command and assign its output. Commented Jul 30, 2013 at 0:04
  • ip is a command on my system. Could that be the problem? Commented Jul 30, 2013 at 0:08
  • @PaulTomblin No. Shell variables and command names don't conflict with each other. I'll bet anything the problem is CR characters in his script. Commented Jul 30, 2013 at 0:39

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.