开发者

Why does this if statement fail in bourne shell?

开发者 https://www.devze.com 2022-12-19 07:10 出处:网络
I\'m learning shell scripting but I can\'t work out this error, any help much appreciated. #!/bin/sh LOCATION=/tmp/loc

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-tom开发者_JS百科cat-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)


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.


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


Another way to write

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

is

[ -d $LOCATION ] || mkdir $LOCATION
0

精彩评论

暂无评论...
验证码 换一张
取 消