Intercepting STDERR

P

Phrogz

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

Daniel Harple

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
 
A

ara.t.howard

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

harp:~ > gem install session >/dev/null 2>&1 && echo 'success'
success


harp:~ > cat a.rb
require 'rubygems'
require 'session'
sh = Session::new
command = %Q( ruby -e" STDERR.puts :stderr; STDOUT.puts :stdout " )
stdout, stderr = sh.execute command

require 'yaml'
y "stdout" => stdout
y "stderr" => stderr
y "sh.status" => sh.status


harp:~ > ruby a.rb
 
P

Phrogz

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?
 
A

ara.t.howard

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?

absolutely. basically what you're after is open3 - it's in the stdlib and
will probably suffice. session is overkill if you're not running multiple
commands per session (no pun intended) anyhow.

anyhow, here's the essence:

harp:~ > cat a.rb
def spawn command, opts = {}
require 'open3'
stdin = opts.values_at:)stdin, 'stdin', 0).compact.first
stdout = opts.values_at:)stdout, 'stdout', 1).compact.first
stderr = opts.values_at:)stderr, 'stderr', 2).compact.first

Open3::popen3(command) do |i,o,e|
i << stdin if stdin
i.close # important!
o.each{|line| stdout << line} if stdout
e.each{|line| stderr << wrine} if stderr
end

$?.exitstatus
end

stdout, stderr = '', ''
exitstatus = spawn 'cat', 0=>42, 1=>stdout, 2=>stderr

require 'yaml'
y 'exitstatus' => exitstatus,
'stdout' => stdout,
'stderr' => stderr


harp:~ > ruby a.rb
---
stdout: "42"
stderr: ""
exitstatus: 0


regards.

-a
 
P

Phrogz

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.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top