getting shell output

M

Matt Harrison

I'm trying to write a script that automates some backups using dump. The
script works well for the most part but I'm having problems getting the
output of dump to send in the email.

I appears that dump writes all of its status output to stderr, and I can
redirect that on the command line using "2>&1" to get it into a file. I'm
unable however to find something similar for ruby.

I've tried backticks (``), %x{}. system() and exec() as well as IO.popen()
but they all fail to intercept the stderr output. I've even tried adding
"2>&1" to the command but it makes no difference.

Interestingly, other programs that output to stderr, such as usage messages
for many programs, will let me capture the stderr output using the same
methods.

Can anyone enlighten me as to why dump is different or another approach I
could take to trap this output?

many thanks

Matt
 
M

Marvin Gülker

Matt said:
I appears that dump writes all of its status output to stderr, and I can
redirect that on the command line using "2>&1" to get it into a file.
I'm
unable however to find something similar for ruby.

I've tried backticks (``), %x{}. system() and exec() as well as
IO.popen()
but they all fail to intercept the stderr output. I've even tried adding
"2>&1" to the command but it makes no difference.

Try open3 from the stdlib:
 
M

Matt Harrison

Try open3 from the stdlib:
--------------------------
require "open3"
Open3.popen3("command") do |stdin, stdout, stderr|
#Your code...
end
---------------------------

Thanks Marvin, that works perfectly. God knows why stderr wasn't getting
intercepted with the others, but with open3 it works beautifully.

Thanks again

Matt
 
W

W. James

Matt said:
I'm trying to write a script that automates some backups using dump.
The script works well for the most part but I'm having problems
getting the output of dump to send in the email.

I appears that dump writes all of its status output to stderr, and I
can redirect that on the command line using "2>&1" to get it into a
file. I'm unable however to find something similar for ruby.

I've tried backticks (``), %x{}. system() and exec() as well as
IO.popen() but they all fail to intercept the stderr output. I've
even tried adding "2>&1" to the command but it makes no difference.

Interestingly, other programs that output to stderr, such as usage
messages for many programs, will let me capture the stderr output
using the same methods.

Can anyone enlighten me as to why dump is different or another
approach I could take to trap this output?

many thanks

Matt

C:\>ruby -e"IO.popen('verify x 2>&1'){|f| p f.read}"
"An incorrect parameter was\nentered for the command.\n"


Tested under windozeXP.

--
 
D

dan

Matt Harrison said:
I've tried backticks (``), %x{}. system() and exec() as well as IO.popen()
but they all fail to intercept the stderr output. I've even tried adding
"2>&1" to the command but it makes no difference.

open3, as others have referred you to, is most probably the Right Thing.
However, I'm puzzled why 2>&1 doesn't work for you

irb(main):004:0> %x{/sbin/dump -0 /boot}
DUMP: Date of this level 0 dump: Sun Dec 13 22:42:35 2009
DUMP: Dumping /dev/sdi1 (/boot) to /dev/tape
DUMP: Cannot open /dev/sdi1
DUMP: The ENTIRE dump is aborted.
=> ""
irb(main):005:0> %x{/sbin/dump -0 /boot 2>&1}
=> " DUMP: Date of this level 0 dump: Sun Dec 13 22:42:42 2009\n DUMP: Dumping /dev/sdi1 (/boot) to /dev/tape\n DUMP: Cannot open /dev/sdi1\n DUMP: The ENTIRE dump is aborted.\n"

Perhaps it's opening /dev/tty instead of writing to stderr, but in that
case I see nothing in the open3 docs that indicate it should fare any
better than 2>&1 does


-dan
 

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,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top