redirect stdout

T

T. Onoma

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

Suggestions?

Thanks,
-t0
 
R

Reimer Behrends

T. Onoma ([email protected]) said:
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.

Reimer Behrends
 
T

T. Onoma

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



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


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

-Mark

Perfecto!

You are an astute reader, Mark. :)

Thank you,
-T0
 
E

Eric Schwartz

Mark J. Reed said:
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. :)

-=Eric
 
R

Reimer Behrends

Mark J. Reed ([email protected]) 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.
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
 
T

T. Onoma

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



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


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

-Mark

Thanks!

-t0
 

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
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top