Getting STDOUT, STDERR, and response code from external *nix command in perl

后端 未结 4 1528
别跟我提以往
别跟我提以往 2020-11-28 16:26

I want to execute an external command from within my Perl script, putting the output of both stdout and stderr into a $variable of my choice, and to get the com

4条回答
  •  一个人的身影
    2020-11-28 17:24

    STDERR is intended to be used for errors or messages that might need to be separated from the STDOUT output stream. Hence, I would not expect any STDERR from the output of a command like less.

    If you want both (or either) stream and the return code, you could do:

    my $out=qx($cmd 2>&1);
    my $r_c=$?
    print "output was $out\n";
    print "return code = ", $r_c == -1 ? $r_c : $r_c>>8, "\n";
    

    If the command isn't executable (perhaps because you meant to use less but wrote lsss instead), the return code will be -1. Otherwise, the correct exit value is the high 8-bits. See system.

提交回复
热议问题