Capturing stdout from cmd while letting it print it

Hi,

I want to capture the output from a program but still have it print to
STDOUT.
I tried

r = []
IO.popen(“mycmd”) do |pipe|
while (line = pipe.gets)
r << line
STDOUT.puts line
end
end

but the output will only come after the command has finished running.
This doesn’t
work here since the command runs for very long and the user will want
the output.

Is there a pure-Ruby way (that works on Windows!) for capturing the
output of
a command without “disturbing” its normal output to STDOUT?

Sorry if this is very basic but I have a windows background… :wink:

Regards,

Karsten

Hi,

···

At Wed, 18 Sep 2002 16:10:20 +0900, coma_killen@fastmail.fm wrote:

I want to capture the output from a program but still have it print to
STDOUT.
I tried

r =
IO.popen(“mycmd”) do |pipe|
while (line = pipe.gets)
r << line
STDOUT.puts line
end
end

but the output will only come after the command has finished running.
This doesn’t
work here since the command runs for very long and the user will want
the output.

“mycmd” may buffer and not flush the output. How large is the
output?

Or, it may send output to stderr or console instead of stdout.


Nobu Nakada