views:

69

answers:

1

I have a shell script that launches a Maven exec:java process -

exec mvn exec:java -Dexec.mainClass=... -Dexec.args="$*"

Now sadly if I run

./myMagicShellScript arg1 "arg 2"

the single string arg 2 doesn't make it through as a single argument as I'd like.

Any thoughts as to how to escape / pass things through properly (perferably in a clean way)?

+2  A: 

I took a look at the mvn script and did some testing. This is what I came up with:

Try changing your script to look like this:

args=(${@// /\\ })
exec mvn exec:java -Dexec.mainClass=... -Dexec.args="${args[*]}"

That changes all spaces which are within each array element to be escaped with a backslash.

Dennis Williamson
When I do that, Maven tries to interpret the rest of the command line instead of passing it in as exec.args :(
Steven Schlansker
What does it do if you pass it explicit arguments: `...-Dexec.args="arg1 'arg 2'"`? Are the single quotes included in the value? If so is that desirable? What about `...-Dexec.args="arg1 arg\ 2"`?
Dennis Williamson
Wow, this is awful. a 'b c' -> "a", "'b", "c'". a b\ c seems to work though.
Steven Schlansker
@Steven: is `mvn` a script or a binary?
Dennis Williamson
It's a shell script that does this strange magic:QUOTED_ARGS=""while [ "$1" != "" ] ; do QUOTED_ARGS="$QUOTED_ARGS \"$1\"" shiftdone
Steven Schlansker
@Steven: see my edit.
Dennis Williamson
Now *that* is some black magic. I hate shell scripting :-p
Steven Schlansker
@Steven: So I assume that means it worked. You're quick!
Dennis Williamson
Yup, like a charm. I can't help but worry that it will fail disastrously in the most important time when encountering particularly strange characters coming in, but so far it handled the admittedly cursory testing admirably.
Steven Schlansker