How to know if $stderr is reopened to /dev/null

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, how could I know if $stderr has been redirected to /dev/null?
This is, imagine a program does:

$stderr.reopen "/dev/null"

And later I want to check if it's redirected to /dev/null or not.

I've found a method which could work:

irb $stderr.inspect
"#<IO:/dev/null>"

So I could parse the resulting string. However it doesn't seem a very "cool=
"=20
approach. Is there any there any other more ellegant way?

Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
J

Justin Collins

Iñaki Baz Castillo said:
Hi, how could I know if $stderr has been redirected to /dev/null?
This is, imagine a program does:

$stderr.reopen "/dev/null"

And later I want to check if it's redirected to /dev/null or not.

I've found a method which could work:

irb $stderr.inspect
"#<IO:/dev/null>"

So I could parse the resulting string. However it doesn't seem a very "cool"
approach. Is there any there any other more ellegant way?

Thanks a lot.

You can use $stderr.tty? if you just need to know if a terminal is
listening.

-Justin
 
I

Iñaki Baz Castillo

El S=C3=A1bado, 16 de Enero de 2010, Justin Collins escribi=C3=B3:
=20
You can use $stderr.tty? if you just need to know if a terminal is
listening.

Great! It also works in case $stderr is redirected to a file in which case=
=20
$stderr.tty? also returns false :)

Thanks.

=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
I

Iñaki Baz Castillo

El S=E1bado, 16 de Enero de 2010, Steven Parkes escribi=F3:
=20
On anything sufficiently Unix-like, you could fstat the filehandle and st= at
the path (/dev/null) and check the major and minor device numbers.

Thanks. However $stderr.tty? seems simpler for my requeriments :)=20


=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
S

Steven Parkes

Hi, how could I know if $stderr has been redirected to /dev/null?

On anything sufficiently Unix-like, you could fstat the filehandle and =
stat the path (/dev/null) and check the major and minor device numbers.=
 
S

Seebs

Hi, how could I know if $stderr has been redirected to /dev/null?

You couldn't.

Consider a wrapper:

exec 5>/dev/null
ruby foo.rb 2>&5

Or for that matter, a file "/home/ibc/nulldev" which happens to be
a character special device using the same driver as /dev/null.

.... Note that this assumes that what you care about is "$stderr is
being thrown away", not the exact path by which it's being done.

-s
 
I

Iñaki Baz Castillo

El S=E1bado, 16 de Enero de 2010, Seebs escribi=F3:
=20
You couldn't.
=20
Consider a wrapper:
=20
exec 5>/dev/null
ruby foo.rb 2>&5
=20
Or for that matter, a file "/home/ibc/nulldev" which happens to be
a character special device using the same driver as /dev/null.
=20
... Note that this assumes that what you care about is "$stderr is
being thrown away", not the exact path by which it's being done.

Thanks for the clarification. However it's enough for me using $stderr.tty?=
=2E=20
This is, I just care when $stederr is not pointing to a terminal.

=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
R

Robert Klemme

El Sábado, 16 de Enero de 2010, Seebs escribió:

Thanks for the clarification. However it's enough for me using $stderr.tty?.
This is, I just care when $stederr is not pointing to a terminal.

Note though, that this also includes cases where stderr is redirected to
a pipe.

robert@fussel:~$ ruby19 -e 'p $stderr.tty?'
true
robert@fussel:~$ ruby19 -e 'p $stderr.tty?' 2>&1 | cat
false
robert@fussel:~$

Kind regards

robert
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top