Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Default shell for Kernel::system() ?
Ronald Fischer
8/1/2006 8:16:00 AM
Ist it possible to kind of define a default shell which will be used
for Kernel::system? I often need to invoke from my Ruby scripts
external commands, involving redirection and globbing, for example
(bash syntax)
foo [a-z]* 2>&-
bar <<<baz
etc. Since these syntactic elements are shell specific, I need to
specify the shell somehow, for example:
system("bash --norc -c 'foo [a-z]* 2>&-'")
system("bash --norc -c 'bar <<<baz'")
which is a bit cumbersome. Of course one obvious solution would be
to define a new function:
def bash(cmd)
system("bash --norc -c '"+cmd+"'")
end
And use instead
bash('foo [a-z]* 2>&-')
bash('bar <<<baz')
But this solution has two drawbacks:
First one has to pay special attention that the shell command itself
should no contain single quotes. For example, it is all too tempting
to write a call
bash("my_command 'one argument'")
which looks correct at first sight, but will have a different
effect than
bash('my_command "one argument"')
due to the interference between Ruby- and bash quoting.
Another drawback of this solution is that one has to include the
definition of the "bash" function with each Ruby script.
So I wonder whether there is may be a simple, builtin solution
for this problem - say, a way to specify the "default shell" used
for system().
Ronald
--
Ronald Fischer <ronaldf@eml.cc>
Posted via
http://www.news...
2 Answers
Logan Capaldo
8/1/2006 12:35:00 PM
0
On Aug 1, 2006, at 4:20 AM, Ronald Fischer wrote:
> Ist it possible to kind of define a default shell which will be used
> for Kernel::system? I often need to invoke from my Ruby scripts
> external commands, involving redirection and globbing, for example
> (bash syntax)
>
> foo [a-z]* 2>&-
> bar <<<baz
>
> etc. Since these syntactic elements are shell specific, I need to
> specify the shell somehow, for example:
>
> system("bash --norc -c 'foo [a-z]* 2>&-'")
> system("bash --norc -c 'bar <<<baz'")
>
> which is a bit cumbersome. Of course one obvious solution would be
> to define a new function:
>
> def bash(cmd)
> system("bash --norc -c '"+cmd+"'")
> end
>
> And use instead
>
> bash('foo [a-z]* 2>&-')
> bash('bar <<<baz')
>
> But this solution has two drawbacks:
>
> First one has to pay special attention that the shell command
> itself should no contain single quotes. For example, it is all too
> tempting
> to write a call
>
> bash("my_command 'one argument'")
>
> which looks correct at first sight, but will have a different
> effect than
>
> bash('my_command "one argument"')
>
> due to the interference between Ruby- and bash quoting.
>
> Another drawback of this solution is that one has to include the
> definition of the "bash" function with each Ruby script.
>
> So I wonder whether there is may be a simple, builtin solution
> for this problem - say, a way to specify the "default shell" used
> for system().
>
> Ronald
>
> --
> Ronald Fischer <ronaldf@eml.cc>
> Posted via
http://www.news...
>
>
I'm pretty sure the "default" shell for system is /bin/sh on a *nix.
Nobuyoshi Nakada
8/1/2006 1:47:00 PM
0
Hi,
At Tue, 1 Aug 2006 17:20:25 +0900,
Ronald Fischer wrote in [ruby-talk:205370]:
> Ist it possible to kind of define a default shell which will be used
> for Kernel::system? I often need to invoke from my Ruby scripts
/bin/sh is hardcoded, like as make, cron and so on.
> external commands, involving redirection and globbing, for example
> (bash syntax)
>
> foo [a-z]* 2>&-
Closing descriptor should work on all variants of Bourne shell,
including UNIX Seventh Edition's.
http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=Unix+Seventh+Edition&f...
Input output.
<&- The standard input is closed. Similarly for the standard output
using >.
If one of the above is preceded by a digit then the file descriptor
created is that specified by the digit (instead of the default 0 or 1).
For example,
... 2>&1
creates file descriptor 2 to be a duplicate of file descriptor 1.
> bar <<<baz
In Ruby, you can:
IO.popen("bar", "w") {|f| f.puts "baz"}
> def bash(cmd)
system("bash", "--norc", "-c", cmd)
> end
--
Nobu Nakada
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Default shell for Kernel::system() ?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password