wildnature has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, here is my code snippet

 use strict;  use warnings;  `ifconfig 2>&1`

This means redirect STDERR to STDOUT, but why this code make ifconfig no output in screen?

This is my understanding: STDERR is null, so STDOUT is "nulled" too, which makes it no display on screen, I am not very sure about it

Replies are listed 'Best First'.
Re: how does '2>&1' work ?
by cdarke (Prior) on Feb 21, 2011 at 08:37 UTC
    You are confusing two different mechanisms here.

    2>&1 is a shell directive, it is nothing to do with Perl. It means: "redirect file descriptor 2 to file descriptor 1". The & indicates a file descriptor rather than a file called "1". File descriptors are indexes to a process's open files and point to kernel's internal tables. 0 (zero) is STDIN, 1 is STDOUT and 2 is STDERR. This is the case on UNIX/Linux systems and on Windows console programs. Shells like the Korn shell (ksh), Bash, and Bourne shell (sh) support this notation, and even cmd.exe and Powershell on Windows. C shell (csh or tcsh) does not, it requires a different syntax.

    As others have said, `backticks` or qx capture STDOUT, which is this case includes STDERR output, however you are not capturing it. Usually you would do something like this:
    use strict; use warnings; my $capture = `ifconfig 2>&1`;
    This enables us to inspect the output (in $capture in this case. If you want the output displayed on the screen then use system instead. If you want both then just print $capture

      But I do encounter such a question:

      case 1:

       use strict;  my $cmd = "dumpcap -i eth0 -b duration:10 -w \\media\\Work\\abc.pcap";  `$cmd`

      "dumpcap" itself will print some infomation, if I add 2>&1 behind $cmd, such as `$cmd 2>&1`, no infomation displayed. But if I do not add 2>&1, information do displays

      in another case, If I change $cmd to :

       my $cmd = "ifconfig";

      There is no information displayed in shell no matter If I add 2>&1 or not

      Why is this difference ?

        That means that dumpcap printed some information to STDERR and ifconfig didn't.
        Why is this difference ?

        Why is dumpcap different from ifconfig? Because it wants to be different?

Re: how does '2>&1' work ?
by Eliya (Vicar) on Feb 21, 2011 at 07:44 UTC
    but why this code make ifconfig no output in screen?

    Normally, backticks capture stdout only, so stderr goes to the terminal, but if you redirect stderr to stdout, both are captured (you do ignore the captured output in the Perl program, though).

    That said, under normal circumstances (no error), ifconfig prints to stdout anyway... — so, without the 2>&1 redirection, you'd get no output either:

    $ perl -e '`ifconfig`' $
Re: how does '2>&1' work ?
by Anonymous Monk on Feb 21, 2011 at 07:44 UTC
    This is my understanding: STDERR is null, so STDOUT is "nulled" too, which makes it no display on screen, I am not very sure about it

    No, `` aka qx , captures STDOUT, and since your program does not print the result of qx ( print `...`; ), nothing is displayed