Default shell for Kernel::system()?

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

On Aug 1, 2006, at 4:20 AM, Ronald F. wrote:

And use instead

definition of the “bash” function with each Ruby script.

I’m pretty sure the “default” shell for system is /bin/sh on a *nix.

Hi,

At Tue, 1 Aug 2006 17:20:25 +0900,
Ronald F. 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&format=html

   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)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs