Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

thorny script argument escaping problem



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?

Two obvious things you could do:
1) Within your script, add quotes around every arg.  This would work for
your example (putting quotes around '--description' doesn't hurt anything),
but you will horribly mangle things if the quote type you choose to use are
already embedded in the arguments passed to your script.

2) Don't use system().  Even if the ARGV variable preserves the quoted
arguments as single entities, system() can't determine how the argument list
was broken up originally since it is only passed a single string.  Use an
fork/exec combination, which expects a broken-up argument list.

Matt




BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org