Redirect stdout

I need to temporarily redirect standard output to nowhere. Not sure how to do.

Suggestions?

Thanks,
-t0

Assuming you run some *IX:

STDOUT.reopen "/dev/null", "w"

will redirect the standard output (including not just Ruby output,
but even the output of subprocesses) to /dev/null.

		Reimer Behrends
···

T. Onoma (transami@runbox.com) wrote:

I need to temporarily redirect standard output to nowhere. Not sure
how to do.

But it also loses the original STDOUT, and M Onoma did say that the
redirection was to be temporary. You can accomplish that by saving
the original value and restoring it later, like this:

old_stdout = STDOUT.dup
STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
. . .
STDOUT.reopen(old_stdout)

Or, if you aren’t invoking any processes external to ruby, you can leave
STDOUT alone and just change $stdout:

$stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
. . .
$stdout = STDOUT

-Mark

···

On Mon, Nov 24, 2003 at 03:49:21PM +0000, Reimer Behrends wrote:

T. Onoma (transami@runbox.com) wrote:

I need to temporarily redirect standard output to nowhere. Not sure
how to do.

Assuming you run some *IX:

STDOUT.reopen “/dev/null”, “w”

will redirect the standard output (including not just Ruby output,
but even the output of subprocesses) to /dev/null.

Minor correction: forgot the mode on my open calls.
Add , “w” to both of the below:

STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )
STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

$stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )
$stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

-Mark

···

On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:

“Mark J. Reed” markjreed@mail.com writes:

Or, if you aren’t invoking any processes external to ruby, you can leave
STDOUT alone and just change $stdout:

Okay, this hints at the answer to a question I’ve been wondering about
for a while now: what’s the difference between STDOUT/STDERR/STDIN and
$stdout/$stderr/$stdin?

It seems your message implies that STDOUT/STDERR/STDIN are IO objects
that when changed, the changes are passed on to children, but if you
change $stdout and friends, those changes are not passed on to
children.

If so, this confuses me greatly, since $stdout prints to stdout, and
if open filehandles (including the magic 3) are inherited by children
(as they are on most *nices), then how can I reopen $stdout without
changing stdout? Why is there such a (to me) confusing and needless
distinction?

Of course, if I inferred incorrectly, disregard the previous
paragraph. :slight_smile:

-=Eric

···


Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
– Blair Houghton.

[…]

But it also loses the original STDOUT, and M Onoma did say that the
redirection was to be temporary.

Good catch. I missed that one.

You can accomplish that by saving
the original value and restoring it later, like this:

old_stdout = STDOUT.dup
STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )
. . .
STDOUT.reopen(old_stdout)

You need

old_stdout.close

here. Otherwise you have a file descriptor leak. Similarly:

Or, if you aren’t invoking any processes external to ruby, you can leave
STDOUT alone and just change $stdout:

$stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )
. . .
$stdout.close
$stdout = STDOUT

(And, of course, add the “w” flag to the open call, as you had already
mentioned.)

	Reimer Behrends
···

Mark J. Reed (markjreed@mail.com) wrote:

Perfecto!

You are an astute reader, Mark. :slight_smile:

Thank you,
-T0

···

On Monday 24 November 2003 05:42 pm, Mark J. Reed wrote:

Minor correction: forgot the mode on my open calls.
Add , “w” to both of the below:

On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:

STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )

STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

$stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )

$stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

-Mark

Right back at you for catching the fd leaks in my examples. Whups. :slight_smile:

Thanks.

-Mark

···

On Wed, Nov 26, 2003 at 12:32:02AM +0000, Reimer Behrends wrote:

Mark J. Reed (markjreed@mail.com) wrote:
[…]

But it also loses the original STDOUT, and M Onoma did say that the
redirection was to be temporary.

Good catch. I missed that one.

Thanks!

-t0

···

On Monday 24 November 2003 05:42 pm, Mark J. Reed wrote:

Minor correction: forgot the mode on my open calls.
Add , “w” to both of the below:

On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:

STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )

STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

$stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )

$stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

-Mark