Getting shell output

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

Matt Harrison wrote:

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:

···

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

Marvin
--
Posted via http://www.ruby-forum.com/\.

Matt Harrison wrote:

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.

···

--

Matt Harrison <iwasinnamuknow@genestate.com> writes:

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

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

···

On Mon, Dec 14, 2009 at 04:34:20AM +0900, Marvin G??lker wrote:

Matt Harrison wrote:
> 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:
--------------------------
require "open3"
Open3.popen3("command") do |stdin, stdout, stderr|
  #Your code...
end
---------------------------