views:

120

answers:

3

I'm learning shell scripting but I can't work out this error, any help much appreciated.

#!/bin/sh
LOCATION=/tmp/loc
PROXY=http://wwwproxy.unimelb.edu.au:8000

http_proxy=$PROXY; export http_proxy
echo "Installing into" $LOCATION

if [ ! -d $LOCATION ]; then mkdir $LOCATION; fi
if [ ! -d $LOCATION/packages ]; then mkdir $LOCATION/packages; fi

TOMCAT_FILE=apache-tomcat-6.0.24.tar.gz
if [ ! -e $LOCATION/packages/$TOMCAT_FILE ]; then
    echo "Downloading" $TOMCAT_FILE
    wget http://mirror.olnevhost.net/pub/apache/tomcat/tomcat-6/v6.0.24/bin/apache-tomcat-6.0.24.tar.gz -O $LOCATION/packages/$TOMCAT_FILE
else
    echo "Found" $TOMCAT_FILE
fi

It appears to be failing on the third if statement with the error:

bash-3.00$ ./install.sh 
Installing into /tmp/loc
./install.sh: test: argument expected

(And for those who think they know better how to do this, no I really do have to write a script to take care of this due to external requirements)

+2  A: 

I assume that your /bin/sh does not point to the bourne-again shell, as the script works for other people (see comments on your question).

The shebang should be /bin/bash for bourne-again shell scripts, but /bin/sh will work in some distributions (like Debian) which have a symlink /bin/sh -> /bin/bash.

AndiDog
I was trying to not use bash, but that explains what is going on! (: Thanks!
corydoras
+2  A: 

unless your sh is not aliased to bash, you can try changing the shebang to #!/bin/bash

ghostdog74
+1  A: 

Another way to write

if [ ! -d $LOCATION ]; then mkdir $LOCATION; fi

is

[ -d $LOCATION ] || mkdir $LOCATION
glenn jackman