Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Intercepting STDERR

Reply
Thread Tools

Intercepting STDERR

 
 
Phrogz
Guest
Posts: n/a
 
      03-01-2006
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.)

 
Reply With Quote
 
 
 
 
Daniel Harple
Guest
Posts: n/a
 
      03-01-2006
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


 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      03-01-2006
That'll do for now, thanks!

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      03-01-2006
On Thu, 2 Mar 2006, Phrogz wrote:

> 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
---
stdout: |
stdout

---
stderr: |
stderr

---
sh.status: 0


-a

--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama


 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      03-01-2006

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)




 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      03-02-2006
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?

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      03-02-2006
On Thu, 2 Mar 2006, Phrogz wrote:

> 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:open3(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

--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama


 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      03-04-2006
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:open3( "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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Intercepting what ASP.NET renders: how fine-grained? ASP .Net 4 08-03-2005 02:36 AM
Intercepting and updating page output... Stu ASP .Net 1 02-22-2005 02:13 PM
Intercepting actions in Internet Explorer (When a user tries to downloada file) Timothy Parez ASP .Net 0 03-06-2004 10:04 AM
Intercepting the rendered output before it goes to the client Nick ASP .Net 2 01-15-2004 09:24 PM
Re: Intercepting and commandeering an HTTP connection Al ASP .Net 1 07-01-2003 09:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57