10

I would like Perl to write to STDERR only if STDOUT is not the same. For example, if both STDOUT and STDERR would redirect output to the Terminal, then I don't want STDERR to be printed.

Consider the following example (outerr.pl):

#!/usr/bin/perl

use strict;
use warnings;

print STDOUT "Hello standard output!\n";
print STDERR "Hello standard error\n" if ($someMagicalFlag);
exit 0

Now consider this (this is what I would like to achieve):

bash $ outerr.pl
Hello standard output!

However, if I redirect out to a file, I'd like to get:

bash $ outerr.pl > /dev/null
Hello standard error

and similary the other way round:

bash $ outerr.pl 2> /dev/null
Hello standard output!

If I re-direct both out/err to the same file, then only out should be displayed:

bash $ outerr.pl > foo.txt 2>&1
bash $ cat foo.txt
Hello standard output!

So is there a way to evaluate / determine whether OUT and ERR and are pointing to the same “thing” (descriptor?)?

3
  • STDERR and STDOUT have their own descriptors, namely 1 and 2. They both print to console generally too.
    – squiguy
    Commented Mar 14, 2013 at 18:32
  • 2
    I suspect that what you really should do is take a step back and reconsider the reasons why you want this. There's almost certainly a better way to accomplish your real goal, whatever it might be. Commented Mar 14, 2013 at 18:59
  • 1
    I'm with Ilmari Karonen. I can't really see why you would be concerned about that. It shouldn't be for duplicating messages. STDOUT and STDERR should inherently be different. Nominal messages go to STDOUT, and error or warning messages go to STDERR. Neither should over lap...ever.
    – titanofold
    Commented Mar 14, 2013 at 19:06

2 Answers 2

12

On Unix-style systems, you should be able to do:

my @stat_err = stat STDERR;
my @stat_out = stat STDOUT;

my $stderr_is_not_stdout = (($stat_err[0] != $stat_out[0]) ||
                            ($stat_err[1] != $stat_out[1]));

But that won't work on Windows, which doesn't have real inode numbers. It gives both false positives (thinks they're different when they aren't) and false negatives (thinks they're the same when they aren't).

3
  • That’s certainly what I would do.
    – tchrist
    Commented Mar 14, 2013 at 21:00
  • +1 for pointing out it's not portable, and still useful to know. Commented Mar 14, 2013 at 22:59
  • Thank you @cjm. This is EXACTLY what I'm looking for. ...What's Windows, btw? ;-)
    – h q
    Commented Mar 15, 2013 at 5:07
5

You can do that (almost) with -t:

-t STDERR

will be true if it is a terminal, and likewise for STDOUT.

This still would not tell you what terminal, and if you redirect to the same file, you may stilll get both.

Hence, if

-t STDERR && ! (-t STDOUT) || -t STDOUT && !(-t STDERR)

or shorter

-t STDOUT ^ -t STDERR  # thanks to @mob

you know you're okay.

EDIT: Solutions for the case that both STDERR and STDOUT are regular files:

Tom Christianson suggested to stat and compare the dev and ino fields. This will work in UNIX, but, as @cjm pointed out, not in Windows.

If you can guarantee that no other program will write to the file, you could do the following both in Windows and UNIX:

  1. check the position the file descriptors for STDOUT and STDERR are at, if they are not equal, you redirected one of them with >> to a nonempty file.
  2. Otherwise, write 42 bytes to file descriptor 2
  3. Seek to the end of file descriptor 1. If it is 42 more than before, chances are high that both are redirected to the same file. If it is unchanged, files are different. If it is changed, but not by 42, someone else is writing there, all bets are off (but then, you're not in Windows, so the stat method will work).
6
  • The question is specifically about detecting when STDOUT and STDERR are the same (file, terminal, or otherwise). Commented Mar 14, 2013 at 18:48
  • @JimStewart WHy do you think I said almost? And warned that it does not work in the case of redirection to the same file? Yet, it is the best you can get without doing something like parsing the output of lsof.
    – Ingo
    Commented Mar 14, 2013 at 18:52
  • 1
    (-t STDOUT) ^ (-t STDERR) will at least tell you if only one stream is going to a terminal. That's a pretty good start.
    – mob
    Commented Mar 14, 2013 at 18:54
  • @mob: I was just about to comment to that effect.
    – titanofold
    Commented Mar 14, 2013 at 18:56
  • 1
    Stat. Check (dev,ino) pair.
    – tchrist
    Commented Mar 14, 2013 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.