Shell type

Under Linux, by default, method Kernel.system(command) (backticks as
well) executes command in the subshell of SH type.

Is there a way to define another shell type (CSH for example) to execute
there, or not?

Alex

Panich, Alexander wrote:

Under Linux, by default, method Kernel.system(command) (backticks as
well) executes command in the subshell of SH type.

Is there a way to define another shell type (CSH for example) to
execute there, or not?

You can always do:

ruby -e 'system "/bin/zsh", "-c", "ls bin/**/*.rb"'

But I'm sure there are other ways, too.

Kind regards

    robert

there shouldn't be, according to iso and posix c. you can always do something
like:

   harp:~ > cat a.rb
   def csh_system cmd
     pipe = IO::popen 'csh', 'r+'
     pipe.puts cmd
     pipe.close_write
     buf = pipe.read
     pipe.close_read
     status = $?.exitstatus
     raise SystemCallError::new(status), cmd unless status == 0
     buf
   end

   stdout = csh_system 'echo 42'
   p stdout

   harp:~ > ruby a.rb
   "42\n"

i used to have csh interface in session, but took it out due to some issues
with stdout/stderr separation...

cheers.

-a

···

On Thu, 23 Jun 2005, Panich, Alexander wrote:

Under Linux, by default, method Kernel.system(command) (backticks as
well) executes command in the subshell of SH type.

Is there a way to define another shell type (CSH for example) to execute
there, or not?

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso

===============================================================================

Panich, Alexander wrote:

Under Linux, by default, method Kernel.system(command) (backticks as
well) executes command in the subshell of SH type.

Is this really true?

irb(main):019:0> puts `pstree -a #{$$}`
irb ...
   └─pstree -a 8921
=> nil
irb(main):020:0> Thread.new {system "/usr/local/bin/ruby", "-e", "puts %{foo}; sleep 10"}
=> #<Thread:0xb7d64988 sleep>
irb(main):021:0> foo

irb(main):022:0* puts `pstree -a #{$$}`
irb ...
   ├─pstree -a 8921
   └─ruby -e puts\040%{foo};\040sleep\04010
=> nil

This is linux, too. I don't see any sh invocation. It's not hidden by pstree is it?

Joel VanderWerf wrote:

Panich, Alexander wrote:

Under Linux, by default, method Kernel.system(command) (backticks as
well) executes command in the subshell of SH type.

irb(main):022:0* puts `pstree -a #{$$}`
irb ...
  ├─pstree -a 8921
  └─ruby -e puts\040%{foo};\040sleep\04010
=> nil

This is linux, too. I don't see any sh invocation. It's not hidden by pstree is it?

I think the reason you don't see the shell is that it is exec()d, otherwise this wouldn't work:
irb(main):082:0> `env | grep ^USER | sed s/$USER/me/`
=> "USER=me\n"

Hmm. I did some more poking. Without diving into the code too much
It looks like on most systems, Kernel.system emulates the behavior
of the Posix system() function instead of just calling it. Basically
if the command string has any characters that might need to be
interpreted by the shell (*;< and so on), then ruby fires up /bin/sh
to handle the command, otherwise Ruby just execs the program directly.

     system "ls" # /bin/sh not started
     system "ls *.rb" # /bin/sh started to handle filename expansion"

Note, that in both cases, Ruby is forking *first* and then starting
either the shell or the program.

If you use Kernel#exec then you have to managing forking in the
ruby code.

Gary Wright

···

On Jun 23, 2005, at 2:59 PM, Joel VanderWerf wrote:

Panich, Alexander wrote:

Under Linux, by default, method Kernel.system(command) (backticks as
well) executes command in the subshell of SH type.

Is this really true?

Hi,

At Fri, 24 Jun 2005 03:59:38 +0900,
Joel VanderWerf wrote in [ruby-talk:146295]:

This is linux, too. I don't see any sh invocation. It's not hidden by
pstree is it?

Kernel#system doesn't invoke any sh if multiple strings
arguments are or a single string without shell metacharacters
is given. Instead, forks and execs directly.

···

--
Nobu Nakada