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

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.

Iñaki Baz C. wrote:

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

El Sábado, 16 de Enero de 2010, Justin C. escribió:

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.

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

Thanks.

El Sábado, 16 de Enero de 2010, Steven P.
escribió:> > 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.

Thanks. However $stderr.tty? seems simpler for my requeriments :slight_smile:

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.

On 2010-01-15, Iñaki Baz C. [email protected] wrote:

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

El Sábado, 16 de Enero de 2010, Seebs
escribió:> 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.

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.

On 01/16/2010 01:01 AM, Iñaki Baz C. wrote:

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.

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.

[email protected]:~$ ruby19 -e ‘p $stderr.tty?’
true
[email protected]:~$ ruby19 -e ‘p $stderr.tty?’ 2>&1 | cat
false
[email protected]:~$

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs