# Getting shell output

**URL:** <https://www.ruby-forum.com/t/getting-shell-output/180160>\
**Category:** Ruby\
**Created:** [December 13, 2009, 8:03pm UTC](https://www.ruby-forum.com/t/getting-shell-output/180160 "2009-12-13T20:03:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 13, 2009, 8:03pm UTC](https://www.ruby-forum.com/t/getting-shell-output/180160/1 "2009-12-13T20:03:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 13, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/getting-shell-output/180160/2 "2009-12-13T20:34:18Z")

</div>

Matt H. 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

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 13, 2009, 8:57pm UTC](https://www.ruby-forum.com/t/getting-shell-output/180160/3 "2009-12-13T20:57:20Z")

</div>

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

> ## 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

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 13, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/getting-shell-output/180160/4 "2009-12-13T21:31:16Z")

</div>

Matt H. wrote:

> even tried adding “2\>&1” to the command but it makes no difference.  
> 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.

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 13, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/getting-shell-output/180160/5 "2009-12-13T23:45:51Z")

</div>

Matt H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
