Forum: Ruby Question about run external program in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-05 07:09
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.
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-05 08:01
(Received via mailing list)
Check $?.exitstatus

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

>
> thanks.
> --
> Posted via http://www.ruby-forum.com/.
>
>


--
Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-05 08:06
Andrew Timberlake wrote:
> Check $?.exitstatus
>

I found that $?.exitstatus always return 0.
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-05 08:22
(Received via mailing list)
On Mon, Jan 5, 2009 at 8:08 AM, Zhao Yi <youhaodeyi@gmail.com> wrote:

>
> thanks.
> --
> Posted via http://www.ruby-forum.com/.
>
>
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://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-06 09:26
How can I get stderr in this method?
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-06 09:53
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)
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-07 14:50
Another option is the open4 gem. See
http://www.codeforpeople.com/lib/ruby/open4/open4-...
and scroll down to SAMPLES
This topic is locked and can not be replied to.