# How to catch STDIO STREAM?

**URL:** <https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699>\
**Category:** Ruby\
**Created:** [May 26, 2008, 11:53pm UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699 "2008-05-26T23:53:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 26, 2008, 11:53pm UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699/1 "2008-05-26T23:53:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 26, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699/2 "2008-05-26T23:59:46Z")

</div>

-------- Original-Nachricht --------

> Datum: Tue, 27 May 2008 06:52:55 +0900  
> Von: Cheyne Li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: How to catch STDIO STREAM?

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Dear Cheyne,

have a look at this:

[http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html)

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 27, 2008, 12:39am UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699/3 "2008-05-27T00:39:40Z")

</div>

-------- Original-Nachricht --------

> Datum: Tue, 27 May 2008 07:21:55 +0900  
> Von: Cheyne Li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> 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

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 27, 2008, 8:45am UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699/4 "2008-05-27T08:45:39Z")

</div>

On 26.05.2008 23:52, Cheyne Li wrote:

> 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

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 27, 2008, 12:22am UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699/5 "2008-05-27T00:22:24Z")

</div>

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?

Axel E. wrote:

> -------- Original-Nachricht --------
> 
> > Datum: Tue, 27 May 2008 06:52:55 +0900  
> > Von: Cheyne Li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > An: removed\_email\_address@domain.invalid  
> > Betreff: How to catch STDIO STREAM?
> 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Dear Cheyne,
> 
> have a look at this:
> 
> [http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html)
> 
> Best regards,
> 
> Axel

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 27, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699/6 "2008-05-27T16:25:07Z")

</div>

Robert K. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 27, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/how-to-catch-stdio-stream/138699/7 "2008-05-27T17:45:35Z")

</div>

On 27.05.2008 16:23, Sebastian H. wrote:

> Robert K. wrote:
> 
> > foo.exe \> errors.txt
> 
> 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
