[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Executing systems commands

John Maclean

4/24/2008 10:19:00 AM

=begin
Hey chaps.

I'm using an array to find the locations of various commands on a
system. Three versions of the same thing. Two work and one doesn't. Any one
know why?
=end

# here's an array of commands that we want to find
k = %w[ ruby xterm enlightenment emacs]


#why does this work?
k.each do |x|
p system("which #{x}")
end

# and this work
k.each do |x|
p system("whereis #{x}")
end


# but this doesn't
k.each do |x|
p system("type -p #{x}")
end


__END__
Regards,

- jjm

2 Answers

mockturtle

4/24/2008 11:36:00 AM

0



John Maclean ha scritto:

> =begin
> Hey chaps.
>
> I'm using an array to find the locations of various commands on a
> system. Three versions of the same thing. Two work and one doesn't. Any one
> know why?
> =end
>
> # here's an array of commands that we want to find
> k = %w[ ruby xterm enlightenment emacs]
>
>
> #why does this work?
> k.each do |x|
> p system("which #{x}")
> end
>
> # and this work
> k.each do |x|
> p system("whereis #{x}")
> end
>
>
> # but this doesn't
> k.each do |x|
> p system("type -p #{x}")
> end
>
>
> __END__
> Regards,
>

Just a hint (maybe wrong): could this depend on the fact that "type"
is a
shell builtin, while "which" and "whereis" are external commands?

Simon Krahnke

4/24/2008 12:04:00 PM

0

* John Maclean <jayeola@gmail.com> (12:19) schrieb:

> #why does this work?
> k.each do |x|
> p system("which #{x}")
> end
>
> # and this work
> k.each do |x|
> p system("whereis #{x}")
> end

which and whereis are real programs

> # but this doesn't
> k.each do |x|
> p system("type -p #{x}")
> end

whereas type is a shell builtin. On my system the standard shell is dash,
which doesn't recognize the -p switch.

NB: system just returns true or false, the output of the command is not
passed to ruby. Use %x for that.

mfg, simon .... l