How to catch STDIO STREAM?

C

Cheyne Li

Hi, there

How can I capture a error massage from the STD IO?
For instance, I have a program call foo.exe and i run the program
/foo.exe

Then the program prints out some error messages.

How can I write a script that capture the errors and save then into a
file?
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Tue, 27 May 2008 06:52:55 +0900
Von: Cheyne Li <[email protected]>
An: (e-mail address removed)
Betreff: How to catch STDIO STREAM?
Hi, there

How can I capture a error massage from the STD IO?
For instance, I have a program call foo.exe and i run the program
./foo.exe

Then the program prints out some error messages.

How can I write a script that capture the errors and save then into a
file?

Dear Cheyne,

have a look at this:

http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html

Best regards,

Axel
 
C

Cheyne Li

Thank you for your information. I tried, but it only give me result like
#<IO:0xf60e8>

So, is there a way to convert it into string?
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Tue, 27 May 2008 07:21:55 +0900
Von: Cheyne Li <[email protected]>
An: (e-mail address removed)
Betreff: Re: How to catch STDIO STREAM?
Thank you for your information. I tried, but it only give me result like
#<IO:0xf60e8>

So, is there a way to convert it into string?



Dear Cheyne,

yes, there is. Use readlines, like this:


-------------------------------
require "open3"

filenames=%w[file1 file2 file3]
inp,out,err=Open3.popen3("xargs","ls","-l")

filenames.each{|f| inp.puts f}
inp.close

output=out.readlines
errout=err.readlines

puts "sent #{filenames.size} lines of input"
puts "got back #{output.size} lines of output"
puts "these were the errors, if any:"
puts errout
-----------------------------------

Best regards,

Axel
 
R

Robert Klemme

How can I capture a error massage from the STD IO?
For instance, I have a program call foo.exe and i run the program
/foo.exe

Then the program prints out some error messages.

How can I write a script that capture the errors and save then into a
file?

You do not need Ruby for that. At an arbitrary shell prompt:

foo.exe > errors.txt

Kind regards

robert
 
S

Sebastian Hungerecker

Robert said:
foo.exe > errors.txt

I'm sorry, I don't know Windows that well, but wouldn't that redirect stdout
and not stderr? Or maybe it would redirect both, but what I think the OP
wants is to just redirect the errors while still printing the normal messages
to the screen.
 
R

Robert Klemme

I'm sorry, I don't know Windows that well, but wouldn't that redirect stdout
and not stderr?
Correct.

Or maybe it would redirect both, but what I think the OP
wants is to just redirect the errors while still printing the normal messages
to the screen.

He did not mention explicitly which stream he wanted to redirect that's
why I presented this solution. Of course, you can as well redirect
stderr on Windows.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top