[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Question about run external program in Ruby

Zhao Yi

1/5/2009 6:08:00 AM

I use this method to run a external program in ruby:

io=IO.popen("lsa 2>&1")do |f|
while line =f.gets do
puts line
end
end

I can get both stdout and stderr output but how can I get the command
return value?

thanks.
--
Posted via http://www.ruby-....

6 Answers

Andrew Timberlake

1/5/2009 6:59:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Check $?.exitstatus

On Mon, Jan 5, 2009 at 8:08 AM, Zhao Yi <youhaodeyi@gmail.com> wrote:

> I use this method to run a external program in ruby:
>
> io=IO.popen("lsa 2>&1")do |f|
> while line =f.gets do
> puts line
> end
> end
>
> I can get both stdout and stderr output but how can I get the command
> return value?
>
> thanks.
> --
> Posted via http://www.ruby-....
>
>


--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Zhao Yi

1/5/2009 7:05:00 AM

0

Andrew Timberlake wrote:
> Check $?.exitstatus
>

I found that $?.exitstatus always return 0.
--
Posted via http://www.ruby-....

Andrew Timberlake

1/5/2009 7:21:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jan 5, 2009 at 8:08 AM, Zhao Yi <youhaodeyi@gmail.com> wrote:

> I use this method to run a external program in ruby:
>
> io=IO.popen("lsa 2>&1")do |f|
> while line =f.gets do
> puts line
> end
> end
>
> I can get both stdout and stderr output but how can I get the command
> return value?
>
> thanks.
> --
> Posted via http://www.ruby-....
>
>
Try the following:
io=IO.popen("ruby some_file_that_doesnt_exist")do |f|
while line =f.gets do
puts line
end
end
puts $?.exitstatus
#returns - 1

--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Zhao Yi

1/6/2009 8:26:00 AM

0

How can I get stderr in this method?

--
Posted via http://www.ruby-....

Brian Candler

1/6/2009 8:56:00 AM

0

Zhao Yi wrote:
> How can I get stderr in this method?

You are already combining stderr with stdout (2>&1)

To get them separately, see open3.rb in the standard library.

# Open3 grants you access to stdin, stdout, and stderr when running
another
# program. Example:
#
# require "open3"
# include Open3
#
# stdin, stdout, stderr = popen3('nroff -man')
#
# Open3.popen3 can also take a block which will receive stdin, stdout
and
# stderr as parameters. This ensures stdin, stdout and stderr are
closed
# once the block exits. Example:
#
# require "open3"
#
# Open3.popen3('nroff -man') { |stdin, stdout, stderr| ... }

However, since it forks twice, I believe you lose the exit status from
the (grand)child. You also need to be careful not to block on reading
from stdout when the child has written a large amount of data to stderr,
or vice versa. (For example, you could use select, or you could have two
separate threads reading from stdout and stderr)
--
Posted via http://www.ruby-....

Brian Candler

1/7/2009 1:50:00 PM

0

Another option is the open4 gem. See
http://www.codeforpeople.com/lib/ruby/open4/open4-0....
and scroll down to SAMPLES
--
Posted via http://www.ruby-....