To start, here is a simple Java program to print the command-line
args:
public class PrintArgs {
public static void main(String args[]) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
And here is a simple shell script which invokes it:
#!/bin/sh
java PrintArgs $*
The problem comes when the script is invoked with arguments grouped
together in double quotes, as follows:
./printArgs.sh Arg1 "Arg2 Arg3"
produces the following undesirable result:
Arg1
Arg2
Arg3
(because $* strips away the quotes)
the desired result is:
Arg1
Arg2 Arg3
(directly calling java, i.e. java PrintArgs Arg1 "Arg2 Arg3", DOES
produce the desired result, so the problem lies in the shell script)
Meanwhile:
./printArgs.sh Arg1 \"Arg2 Arg3\"
produces the following (also undesirable) result:
Arg1
"Arg2
Arg3"
I've been racking my brains out trying every combination of things to
get this to work: escaped quotes, escaped spaces, "$*", "$@", etc.
etc. I need help badly! I've tried many things, so if possible, please
verify that the answer works before responding.
Thanks,
David |