简体   繁体   中英

Shell Arguments Not Being Passed to Java Class Correctly

I wrote a short shell script that takes in parameters, checks them (because they're "optional" and if they're empty it sets default values), and them passes them to a Java class on the server. Basically, like so:

x=${2:-'Default'}
y=${3:-'Default Name Has Spaces'}
echo \"$x\"
echo \"$y\"
java -d64 -ms3G -mx3G -cp example.jar -Djava.security.policy=policy com.example.JavaClass $1 \"$x\" \"$y\"

However, when the shell makes the call to the Java class, I just get back that (if the above defaults are used) "argument Name is not recognized".

With the escaped-quotes around the echo on lines 3 and 4, I see that the variables ARE set correctly and they're echoed out like so: "Default" and "Default Name Has Spaces" , yet even with the escaped quotes in the Java call, they seem to be passing as more than two arguments -- it's like they're being passed as java -d64 -ms3G -mx3G -cp example.jar -Djava.security.policy=policy com.example.JavaClass $1 Default Default Name Has Spaces" which fails because the Java class only expects three total arguments.

Does anyone have any ideas as to why or how to make the arguments pass correctly?

The problem is that you're quoting the " , which prevents it from quoting the arguments. Just remove the backslashes, and you should be set:

x=${2:-'Default'}
y=${3:-'Default Name Has Spaces'}
echo "$x"
echo "$y"
java -d64 -ms3G -mx3G -cp example.jar -Djava.security.policy=policy com.example.JavaClass $1 "$x" "$y"

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