-1

I have recently Updated from 14.04.3 LTS to 16.04.2 LTS. My shell scipts that got executed on 14 version are failing in the latest version i.e. 16.04.2.
Below are the sample errors:

test: 24: test: function: not found
test: 25: [: Illegal number: !
test.sh: 33: test.sh: Syntax error: "}" unexpected

The lines have the following information in the script

24 line : function CheckErrors {
25 line :  if [ ! $1 -eq 0 ]; 
    then
    echo "****************************************"
    echo "STEP FAILED: $2                         "
    echo "Terminating execution and exiting       "
    echo "****************************************"
    exit 1
  fi
33 line : }

Are there any changes between both the versions?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
sneha
  • 7

1 Answers1

1

Starting off, I would change your scripts syntax to this:

CheckErrors(){
if [ $1 -dt 0 ]; 
    then
    echo "****************************************"
    echo "STEP FAILED: $2                         "
    echo "Terminating execution and exiting       "
    echo "****************************************"
    exit 1
  fi
}

This should make your script less error prone during an update cause:

  • function keyword is optional(and is a "bashism"). Use func(){commandA; commandB } instead. This will make your scripts more portable if a shell change(/bin/sh) occurrs during upgrades;
  • You dont need to valuate if result is not equal zero (if [ ! $1 -eq 0 ]). Evaluate if result is different then(-dt) zero (if [ $1 -dt 0 ]).

My suspect here is that you were using bash shell before the upgrade as the default /bin/sh, since dash needs the () at the end of the function name while for bash is enough to have (), function keyword or both. Take a look at this example of bash script:

#!/bin/bash 
function quit {
    exit
}
function hello {
    echo Hello!
}
hello
quit
echo foo 

The error line test: 24: test: function: not found makes me question if you are really using bash at all, so...

My answer: put the shebang on your scripts pointing to #!/bin/bash and use ./test.sh to run it or follow this Ask Ubuntu solution.

  • Hi I have replaced with the code you sent and below is the response – sneha Jun 16 '17 at 06:07
  • pentaho:~/repository$ sh test.sh test.sh: 9: test.sh: CheckErrors: not found test.sh: 10: test.sh: if[: not found test.sh: 11: test.sh: Syntax error: "then" unexpected – sneha Jun 16 '17 at 06:07
  • You have to run the script with ./test.sh and not sh test.sh after editing shebang on first line.... Have you read my answer at all? –  Jun 20 '17 at 18:58