thorny script argument escaping problem

David Kramer david at thekramers.net
Tue Jun 20 23:27:17 EDT 2006


Robert La Ferla wrote:
> I have a wrapper script written in Ruby (this shouldn't matter - it 
> could be PERL).  This script passes all command-line arguments to a 
> Java program for processing.  The wrapper script should pass all 
> arguments "verbatim" to the Java program.
> 
> Here's the script:
> 
> #!/usr/bin/ruby -w
> 
> cmd = "java MyClass" + " "
> ARGV.each { |arg| cmd.concat arg+' ' }
> system(cmd)
> 
> The problem is that some arguments take a quoted string like:
> 
> mywrapper --description "This is a test."
> 
> The shell is unescaping the arguments so when it calls my Java 
> application, it gets:
> 
> java MyClass --description This is a test.
> 
> Needless to say, this doesn't work.  Furthermore, I cannot require  the
> user to double escape either. e.g.  mywrapper --description  "'This is a
> test'".  I also need this wrapper to work for other  programs which may
> have quite complicated options.  Therefore, it  should work generically.
> 
> Am I missing something obvious?  Suggestions?


Try
ARGV.each { |arg| cmd.concat "'"+arg+"' " }



More information about the Discuss mailing list