[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Intercepting STDERR

Gavin Kistner

3/1/2006 5:39:00 PM

I'm writing a Ruby script to run Lua code for TextMate. Using:
output = `lua #{filename}`
works when the file prints happily, but when an error occurs (e.g. a
syntax error in the file) the error message is printed to stderr (I
think) and 'escapes' my capture.

How/can I catch output to stderr from another system command? (I want
to use the output later, I just don't want it spat out at that point.)

7 Answers

Daniel Harple

3/1/2006 6:14:00 PM

0

On Mar 1, 2006, at 6:43 PM, Phrogz wrote:

> How/can I catch output to stderr from another system command? (I want
> to use the output later, I just don't want it spat out at that point.)

You could do:

output = `command 2>&1`

However, now $stderr is mixed in with $stdout.

-- Daniel


Gavin Kistner

3/1/2006 6:54:00 PM

0

That'll do for now, thanks! :)

Ara.T.Howard

3/1/2006 6:58:00 PM

0

Logan Capaldo

3/1/2006 7:20:00 PM

0


On Mar 1, 2006, at 1:58 PM, Phrogz wrote:

> That'll do for now, thanks! :)
>
>

You may want to check out IO.popen and IO.popen3 (or is it popen2? I
can never remembr the numbers)




Gavin Kistner

3/2/2006 4:57:00 AM

0

That's awesome, ara. Unfortunately, I need to write something that will
work on random MacOS machines witj a default Ruby install. Can I poke
about in the innards of session and steal ideas/code?

Ara.T.Howard

3/2/2006 5:21:00 AM

0

Gavin Kistner

3/4/2006 8:13:00 AM

0

Thanks so much, Ara. That's almost perfect, except that the exitstatus
seems to be eaten. No matter what happens, I get a clean 0 exitstatus.
For example:

require 'open3'

`lua xxx`
#=> lua: cannot open xxx: No such file or directory

p $?.exitstatus
#=> 1

output, errors = '', ''
p Open3::popen3( "lua xxx" ){ |i,o,e|
i.close
o.each { |line| output << line }
e.each { |line| errors << line }
$?.exitstatus
}
#=> 0


I can mostly infer the failure based on whether or not messages came to
stderr, but it'd be nice to know for sure.