Capturing output from Kernel#system

Maybe I’m dumb, I dunno.

I need to execute an external command and capture the output from it in a
Ruby structure.

When I use the Kernel#system method to do this, I get the output just
fine, but I can’t figure out how to capture it. The method returns true,
of course, but obviously true isn’t what I want to capture!

I’d like to avoid the obvious kludge of writing the command output to a
file and reading the file in Ruby. Isn’t there a better way than that? The
pickaxe book isn’t helping me here at all (maybe I didn’t look where I
should have…).

Thanks for any help you can provide.

···


Tim Kynerd Sundbyberg (småstan i storstan), Sweden tim@tram.nu
Sunrise in Stockholm today: 7:31
Sunset in Stockholm today: 16:31
My rail transit photos at http://www.kynerd.nu

I need to execute an external command and capture the output from it in a
Ruby structure.

Probably `` (backquote)

I'd like to avoid the obvious kludge of writing the command output to a
file and reading the file in Ruby. Isn't there a better way than that? The
pickaxe book isn't helping me here at all (maybe I didn't look where I
should have...).

p 415 `(backquote)

Guy Decoux

Maybe I’m dumb, I dunno.

I need to execute an external command and capture the output from it in a
Ruby structure.

When I use the Kernel#system method to do this, I get the output just
fine, but I can’t figure out how to capture it. The method returns true,
of course, but obviously true isn’t what I want to capture!

I’d like to avoid the obvious kludge of writing the command output to a
file and reading the file in Ruby. Isn’t there a better way than that? The
pickaxe book isn’t helping me here at all (maybe I didn’t look where I
should have…).

output = uptime
=> " 19:05:58 up 29 days, 9:51, 1 user, load average: 0.01, 0.04, 0.03\n"
p output
" 19:05:58 up 29 days, 9:51, 1 user, load average: 0.01, 0.04, 0.03\n"
=> nil

···

On Sun, Feb 16, 2003 at 02:55:01AM +0900, Tim Kynerd wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Computers are like air conditioners. Both stop working, if you open windows.
– Adam Heath

Fished around a little more in the pickaxe book and discovered the
IO#popen method, which does the trick. Verrrrrry interesting…

···

On Sat, 15 Feb 2003 18:32:06 +0100, Tim Kynerd wrote:

Maybe I’m dumb, I dunno.

I need to execute an external command and capture the output from it in a
Ruby structure.

When I use the Kernel#system method to do this, I get the output just
fine, but I can’t figure out how to capture it. The method returns true,
of course, but obviously true isn’t what I want to capture!

I’d like to avoid the obvious kludge of writing the command output to a
file and reading the file in Ruby. Isn’t there a better way than that? The
pickaxe book isn’t helping me here at all (maybe I didn’t look where I
should have…).

Thanks for any help you can provide.


Tim Kynerd Sundbyberg (småstan i storstan), Sweden tim@tram.nu
Sunrise in Stockholm today: 7:31
Sunset in Stockholm today: 16:31
My rail transit photos at http://www.kynerd.nu