How can I run an external command and capture its output in Perl?
I'm new to Perl and want to know of a way to run an external 开发者_JAVA百科command (call it prg
) in the following scenarios:
- Run
prg
, get itsstdout
only. - Run
prg
, get itsstderr
only. - Run
prg
, get itsstdout
andstderr
, separately.
You can use the backtics to execute your external program and capture its stdout
and stderr
.
By default the backticks discard the stderr
and return only the stdout
of the external program.So
$output = `cmd`;
Will capture the stdout
of the program cmd and discard stderr
.
To capture only stderr
you can use the shell's file descriptors as:
$output = `cmd 2>&1 1>/dev/null`;
To capture both stdout
and stderr
you can do:
$output = `cmd 2>&1`;
Using the above you'll not be able to differenciate stderr
from stdout
. To separte stdout
from stderr
can redirect both to a separate file and read the files:
`cmd 1>stdout.txt 2>stderr.txt`;
In most cases you can use the qx//
operator (or backticks). It interpolates strings and executes them with the shell, so you can use redirections.
To capture a command's STDOUT (STDERR is unaffected):
$output = `cmd`;
To capture a command's STDERR and STDOUT together:
$output = `cmd 2>&1`;
To capture a command's STDERR but discard its STDOUT (ordering is important here):
$output = `cmd 2>&1 1>/dev/null`;
To exchange a command's STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out the old STDERR:
$output = `cmd 3>&1 1>&2 2>&3 3>&-`;
To read both a command's STDOUT and its STDERR separately, it's easiest to redirect them separately to files, and then read from those files when the program is done:
system("program args 1>program.stdout 2>program.stderr");
You can use IPC::Open3 or IPC::Run. Also, read How can I capture STDERR from an external command from perlfaq8.
Beware about the answer of Eugene (can't comment on his answer), just above, that the syntax to exchange SDTOUT and STDERR is valid on Unixes (Unixen-like shells such as ksh, or bash I guess) but not under Windows CMD (error: 3>& was unexpected at this time.
).
The appropriate syntax under Windows CMD and Perl on Windows is:
perl -e "$r=qx{nslookup 255.255.255.255 2>&1 1>&3 3>&2};
Note that the command:
nslookup 255.255.255.255
will produce (something like) on STDOUT:
Server: mymodem.lan
Address: fd37:c01e:a880::1
and on STDERR:
*** mymodem.lan can't find 255.255.255.255: Non-existent domain
You can test that this syntax works with the following CMD/Perl syntax:
First:
perl -e "$r=qx{nslookup 255.255.255.255 2>&1 1>&3 3>&2}; $r=~s/[\n\r]//eg; print qq{on STDOUT qx result=[$r]};"
you get: Server: mymodem.lan
Address: fd37:c01e:a880::1
on STDOUT qx result=[*** mymodem.lan can't find 255.255.255.255: Non-existent domain]
Then
perl -e "$r=qx{nslookup 255.255.255.255 2>&1 1>&3 3>&2}; $r=~s/[\n\r]//eg; print STDOUT qq{on STDOUT qx result=[$r]};" 2>&1 1>NUL:
you get: Server: mymodem.lan
Address: fd37:c01e:a880::1
QED [fr:CQFD]
Note that it is not possible to get BOTH stderr and stdout as returned string for a qx or backticks command. If you know for sure that the err text returned by your spawned command is of length N lines, you can still redirect STDERR to STDOUT like describe by Eugene and others but capture your qx returned text in an array instead of as scalar string. The STDERR flow will be returned into the array before the STDOUT so that the N first lines of your array are the SDTERR lines. Like:
@r=qx{nslookup 255.255.255.255 2>&1};
$r[0] is "*** mymodem.lan can't find 255.255.255.255: Non-existent domain"
But of course you must be sure that there is an err text on STDERR and of strictly N lines (stored in @r[0..N-1]
). If not, the only solution is using temp files as described above.
精彩评论