thorny script argument escaping problem
Tom Metro
blu at vl.com
Wed Jun 21 01:26:08 EDT 2006
Matthew Gillen wrote:
> Robert La Ferla wrote:
>> The wrapper script should pass all arguments
>> "verbatim" to the Java program.
>> The problem is that some arguments take a quoted string like:
>> The shell is unescaping the arguments...
>
> Two obvious things you could do:
> 1) Within your script, add quotes around every arg. ...
> but you will horribly mangle things if the quote type you choose to use are
> already embedded in the arguments passed to your script.
You could also escape spaces and avoid the quoting issue, although this
introduces other complications, as you don't want to escape spaces
inside quotes that cause the shell not to remove the escaping character.
> 2) Don't use system(). Use an fork/exec combination, which expects a
> broken-up argument list.
This is probably the better approach. Perl's system() will provide the
desired behavior if you pass it a list:
system LIST
Does exactly the same thing as "exec LIST", except that a fork
is done first, and the parent process waits for the child
process to complete. Note that argument processing varies
depending on the number of arguments. If there is more than one
argument in LIST, or if LIST is an array with more than one
value, starts the program given by the first element of the list
with arguments given by the rest of the list. If there is only
one scalar argument, the argument is checked for shell
metacharacters, and if there are any, the entire argument is
passed to the system's command shell for parsing (this is
"/bin/sh -c" on Unix platforms, but varies on other platforms).
If there are no shell metacharacters in the argument, it is
split into words and passed directly to "execvp", which is more
efficient.
So your wrapper becomes (untested):
@cmd = qw/java MyClass/;
system(@cmd, at ARGV);
-Tom
--
Tom Metro
Venture Logic, Newton, MA, USA
"Enterprise solutions through open source."
Professional Profile: http://tmetro.venturelogic.com/
More information about the Discuss
mailing list