[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Reading stdout & stderr from a pipe with popen3

Matt Mencel

7/17/2008 6:14:00 PM


>
> btw - you are simply re-writing the open4 gem - why not use it?
>


Because I already kind of knew Open3 and it took me long enough to figure out how to use that one....and I like to make my life as difficult as possible. :)

So now I'm trying Open4 as you suggest and have a couple questions.

require "rubygems"
require "open4"

pid, stdin, stdout, stderr = Open4::popen4 "zmprov"
stdin.puts "sm shares"
stdin.puts "gaf"
stdin.close
puts stderr.read.strip if !stderr.read.strip.empty?
puts stdout.read.strip if stderr.read.strip.empty?
exit


The check for stderr being empty? doesn't work...it prints a blank line if no error was returned so I tried checking for newline('\n') but it doesn't work either. What do I check for if nothing was returned on stderr?

The other thing I noticed is that I have to include the stdin.close or the program stops. I guess it's waiting for the close? It can't be after I try to print stderr/stdout either...it has to be before I check for those. So I guess I have to send all my input and then close the input and then check the statuses of stdout/stderr to decide how to proceed?

Looks like this will work and there doesn't seem to be any blocking getting in the way. Eventually I'm going to be sending thousands of these commands to the zmprov> prompt so I want this to be a tight and fast as possible. As long as I can get the full stdout/stdin of the last command before moving on to the next account...I think this will work.

Thanks,
Matt