41

I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.

Here's what's happening: I have a skeleton script that calls another script that does most of the work. However, it makes calls to getConfig.sh a lot. getConfig.sh basically just parses some command line argument (using getopts) and calls a Java program to parse some XML files. Anyways, getConfig.sh is throwing up lots of errors (but still seems to work).

Here's the message that I'm getting

getconfig.sh: 89: [[: not found
getconfig.sh: 89: [[: not found
getconfig.sh: 94: [[: not found

I get those three errors every time it runs; however, the script completes and the Java code runs.

Here's the relavent code section

parseOptions $*

if [[ "${debugMode}" == "true" ]] ; then
    DEBUG="-DDEBUG=true"
    echo "${JAVA_HOME}/bin/java ${DEBUG} -Djava.endorsed.dirs=${JAXP_HOME} -jar $(dirname $0)/GetXPath.jar ${XML_File} ${XPath_Query}"
fi

Line 89 is "parseOptions $* and line 94 is "fi"

Thanks for the answers.

Justin
  • 413
  • 1
  • 4
  • 4

3 Answers3

111

If your script is executable and you are executing it like ./getconfig.sh, the first line of your script needs to be:

#!/bin/bash

Without that shebang line, your script will be interpreted by sh which doesn't understand [[ in if statements.

Otherwise, you should run your script like bash getconfig.sh, not sh getconfig.sh. Even if your default shell is bash, scripts run with sh will use a reduced set of bash's features, in order to be more compliant with the POSIX standard. [[ is one of the features that is disabled.

Tom Fenech
  • 69,051
  • 12
  • 96
  • 131
Dennis Williamson
  • 324,833
  • 88
  • 366
  • 429
24

Use:

bash scriptname.sh

instead of:

sh scriptname.sh

Daniel Kmak
  • 17,324
  • 7
  • 67
  • 87
0

If you are checking for equality, shouldn't the if be ?

if [[ "${debugMode}" = "true" ]]; then .... fi

Gangadhar
  • 1,895
  • 9
  • 9