[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Shell type

Panich, Alexander

6/23/2005 2:45:00 PM

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



6 Answers

Robert Klemme

6/23/2005 2:54:00 PM

0

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



Ara.T.Howard

6/23/2005 3:22:00 PM

0

Joel VanderWerf

6/23/2005 7:00:00 PM

0

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?


Pete Elmore

6/23/2005 7:26:00 PM

0

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"


Gary Wright

6/23/2005 10:05:00 PM

0


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?

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



nobu.nokada

6/23/2005 11:37:00 PM

0

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