简体   繁体   中英

What is wrong with my shell script in SunOS, runs fine on other linux flavours

Please can someone identify what is the problem in my shell script, it works fine on other Linux systems except on Sunos below is my output

drifter% cat run.sh
#!/bin/sh -x
if [ ! $JAVA_HOME ] || [ $JAVA_HOME == "" ]
then
    echo Enter path to JAVA HOME:
    read JAVA_HOME
fi

if [ ! -f $JAVA_HOME/bin/java ]
then
echo "JAVA_HOME variable does not point to a valid java instance"
exit 1
fi

echo "Using JAVA_HOME: "$JAVA_HOME
JAVA_BIN=$JAVA_HOME/bin
ver=`$JAVA_HOME/bin/java -version 2>&1 | head -1 | awk '{print $NF}' | cut -d'.' -f2`

if [ $ver -ge 5 ]
then
    JAVA_LIB=`pwd`/lib
    export JAVA_LIB

    $JAVA_BIN/java -cp ./lib/a-jdbc-sqlserver-4.2.1.jar:./lib/a-jdbc-db2-4.2.1.jar:./lib/ilmInit.jar:./lib/db2jcc.jar:./lib/db2jcc_license_cisuz.jar:./lib/db2jcc_license_cu.jar:./lib/csm-runtime-1.0.jar:./lib/csm-dbutil-1.0.jar:./lib/classes12_g.jar:./lib/commons-beanutils-1.8.3.jar:./lib/commons-cli-1.2.jar:./lib/commons-exec-1.1.jar:./lib/log4j-1.2.8.jar:./lib/groovy-all-1.8.1.jar -Dlog4j.configuration=com/a/csm/log4j.xml -Dendorsed_plugins_dir=./plugins InitValues $@

else
    echo Current JDK $ver
    echo "Expected JDK 1.5 or later. Please fix your JAVA HOME and try again."
    exit 1
fi
drifter% ./run.sh
+ [ ! ]
./run.sh: test: argument expected
drifter%

Note : I am using csh

Update

I changed "$JAVA_HOME" everywhere

but still i get

drifter% ./run.sh
+ [ ! /home/ilma1/java16/java ]
+ [ /home/ilma1/java16/java ==  ]
./run.sh: test: unknown operator ==

Probably $JAVA_HOME isn't set. An unset variable normally expands to an empty string, so this:

if [ ! $JAVA_HOME ] || [ $JAVA_HOME == "" ]

is equivalent to this:

if [ ! ] || [ == "" ]

which is a syntax error. ( [ is another name for the test command; it's usually a symbolic link.)

Try quoting the variable name:

if [ "$JAVA_HOME" == "" ]

And if you set $JAVA_HOME in response to the prompt, you probably want to export it. (Actually I'm not sure of that; does java depend on $JAVA_HOME being set?)

EDIT :

Ok, it looks like $JAVA_HOME was set.

For the test (or [ ) command, the string equality operator is = , not == .

Try:

if [ "$JAVA_HOME" = "" ]

EDIT2 :

This:

if [ -z "$JAVA_HOME" ]

is probably better (see @nm's answer).

The portable way to check for empty strings is

if [ -n "%VAR" ]  #true if $VAR is non-empty

or

if [ -z "$VAR" ]  # true if $VAR is empty

It is also possible to use if [ "x$VAR" == "x" ] . This form is useful to compare arbitrary string that could be empty, eg if [ "x$JAVA_HOME" == "x$SCALA_HOME" ] .

Th form if [ "$VAR" ] can fail with some older incarnations of shell, though modern SunOS should be OK.

== is not an operator in sh. A single = is the posix compliant test operator.

if [ ! $JAVA_HOME ] || [ $JAVA_HOME = "" ]

I have just had this problem on Solaris 10, and found a great snipped of information in this ten year old post: http://lists.infowares.com/archive/clug/2003-February/001849.html

I just tried 'if [ "a" == "a" ]; then echo yes; fi' at the bourne shell prompt on several different platforms. Solaris said 'test: unknown operator ==' AIX said 'sh: ==: unknown test operator' HP-UX said 'sh:==: A test command parameter is not valid.' IRIX said 'yes' (just to be different :) )

Secondly, it's possible that sh doesn't know what to do with your empty string. An old-school hack is to change your test to

[ XX"$JAVA_HOME" = XX"" ]

which for me compares [ XX/opt/jdk1.7.0_02 = XX ] which is false, rather than doing a test with an empty right hand side which fails in some shells.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM