user2340543
user2340543

Reputation: 1

Solaris/UNIX passing "$*" to compiled java

I am having a strange issue with passing "$*" to a java compiled program. The program will not parse the variables when I pass it from the following command line:

/export/home/checkout>/tmp/jsnmp.sh -f noc2 -t 4,4 -x \"resdiag SilentDiag 1\",18

The "/tmp/jsnmp.sh" contains the following:

#!/bin/sh

$JAVA_HOME/bin/java -jar /export/home/checkout/jsnmp.jar $*

Now if I run this:

$JAVA_HOME/bin/java -jar /export/home/checkout/jsnmp.jar \
     -f noc2 -t 4,4 -x "resdiag SilentDiag 1",18

Everything works.

Any ideas folks?

Upvotes: 0

Views: 81

Answers (2)

denis
denis

Reputation: 447

This has nothing to do with Java or Solaris, this is purely shell stuff.

This is because after $* substitution arguments will get re-parsed and will become separate arguments. E.g. your java executable will see it as

-f noc2 -t 4,4 -x resdiag SilentDiag 1,18

or something like that.

Check out the following test code:

a.sh:

echo $1
echo $2
echo $3
./b.sh $*

b.sh:

echo b
echo $1
echo $2
echo $3

Running it will produce the following output:

$ ./a.sh "1 2" 3
1 2
3

b
1
2
3

See how it was 2 arguments for the first script and 3 params for the second.

Encompassing $* in double quotes won't help, because it will send all arguments as one argument.

The following should work:

#!/bin/sh

$JAVA_HOME/bin/java -jar /export/home/checkout/jsnmp.jar "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"

You will have some max number of arguments though...

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

You probably want to maintain the quoting within the script, so use "$@".

Upvotes: 1

Related Questions