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

自作多情 提交于 2019-11-26 09:14:15

问题


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 command\'s exit code into the $? variable.

I went through solutions in perlfaq8 and their forums, but they\'re not working for me. The strange thing is that I don\'t get the output of sdterr in any case, as long as the exit code is correct.

I\'m using Perl version 5.8.8, on Red Hat Linux 5.

Here\'s an example of what I\'m trying:

my $cmd=\"less\";
my $out=`$cmd 2>&1`;

or

my $out=qx($cmd 2>&1);

or

open(PIPE, \"$cmd 2>&1|\");

When the command runs successfully, I can capture stdout.

I don\'t want to use additional capture modules. How can I capture the full results of the external command?


回答1:


Actually, the proper way to write this is:

#!/usr/bin/perl
$cmd = 'lsss';  
my $out=qx($cmd 2>&1);
my $r_c=$?;
print "output was $out\n";
print "return code = ", $r_c, "\n";

You will get a '0' if no error and '-1' if error.




回答2:


This was exactly the challenge that David Golden faced when he wrote Capture::Tiny. I think it will help you do exactly what you need.

Basic example:

#!/usr/bin/env perl

use strict;
use warnings;

use Capture::Tiny 'capture';

my ($stdout, $stderr, $return) = capture {
  system( 'echo Hello' );
};

print "STDOUT: $stdout\n";
print "STDERR: $stderr\n";
print "Return: $return\n";

After rereading you might actually want capture_merged to join STDOUT and STDERR into one variable, but the example I gave is nice and general, so I will leave it.




回答3:


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.




回答4:


A frequently given answer to this question is to use a command line containing shell type redirection. However, suppose you want to avoid that, and use open() with a command and argument list, so you have to worry less about how a shell might interpret the input (which might be partly made up of user-supplied values). Then without resorting to packages such as IPC::Open3, the following will read both stdout and stderr:

my ($child_pid, $child_rc);

unless ($child_pid = open(OUTPUT, '-|')) {
  open(STDERR, ">&STDOUT");
  exec('program', 'with', 'arguments');
  die "ERROR: Could not execute program: $!";
}
waitpid($child_pid, 0);
$child_rc = $? >> 8;

while (<OUTPUT>) {
  # Do something with it
}
close(OUTPUT);


来源:https://stackoverflow.com/questions/8733131/getting-stdout-stderr-and-response-code-from-external-nix-command-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!