thorny script argument escaping problem

robertlaferla at comcast.net robertlaferla at comcast.net
Wed Jun 21 14:02:34 EDT 2006


That would be nice BUT I would need to dynamically create a list of arguments for the system method.  If I pass in a string (a comma-separated list of quoted arguments), the system() method treats that as ONE argument.  I need to have it treat it as multiple/variable arguments.  I can't seem to find any documentation on how to accomplish that.  Anyone know how to do it?  i.e.  I want to call a method that takes in a variable list of arguments (varargs) but I want to dynamically create that list.  How can it be programmed in Ruby?

 -------------- Original message ----------------------
From: Lars Kellogg-Stedman <lars at larsshack.org>
> > to lead to solutions...  I haven't tried it yet but I think I just
> > need to re-escape strings that have whitespace in them.
> 
> I think you'll be much happier if Ruby offers something like perl's
> system() call -- i.e., a call that if passed a list, bypasses /bin/sh and
> calls exec() directly.  This means you don't have to worry about *any*
> sort of escaping, and there's no possibility of a random shell
> metacharacter causing problems.
> 
> I haven't worked with Ruby before, but my reading of the ruby
> documentation implies that Kernel.system() does work this way.  Compare,
> for example, the output of:
> 
>   Kernel.system("echo *")
> 
> Vs:
> 
>   Kernel.system("echo", "*")
> 
> The first call goes through /bin/sh so the '*' results in glob expansion,
> whereas the second call echoes a literal '*'.  Similarly, this:
> 
>   Kernel.system("touch", 'a "file" with spaces')
> 
> Will create a filename containing spaces and quotes, no escaping
> necessary.
> 
> -- Lars
> 





More information about the Discuss mailing list