How can I run an external command and capture its output in Perl?
Solution 1
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`;
Solution 2
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");
Solution 3
You can use IPC::Open3 or IPC::Run. Also, read How can I capture STDERR from an external command from perlfaq8.
Solution 4
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.
Related videos on Youtube
gameover
Hi I'm Roger.. I Love programming.. SO is a great place.. while(1) have_fun();
Updated on July 09, 2022Comments
-
gameover over 1 year
I'm new to Perl and want to know of a way to run an external command (call it
prg
) in the following scenarios:- Run
prg
, get itsstdout
only. - Run
prg
, get itsstderr
only. - Run
prg
, get itsstdout
andstderr
, separately.
-
FMc over 13 yearsDo yourself a big favor and spend some time on perldoc.perl.org -- for starters the "Tutorials" and "FAQs" areas. Also run this to familiarize yourself with Perl's system for accessing help info on the command-line:
perldoc --help
. The answer to your specific question could have been found directly by command-line searches such as these:perldoc -q capture
orperldoc -q external
.
- Run
-
dave4420 over 13 yearsAnother way to read stdout and stderr separately, without using temporary files, is to use IPC::Open3.
-
William Pursell over 13 yearsEvery time backticks are used, a kitten dies. In shell, use $(). In perl, use qx.
-
Severun over 9 yearsThanks for that, I killed 20 kittens before I used qx ;)
-
L__ over 7 yearsThe second solution
cmd 2>&1 1>/dev/null
kills both stdout and stderr. Somehow the shell doesn't identify that the two redirections are separate!! -
x-yuri about 5 yearsHow do you pass arguments to
cmd
with backticks, ensuring proper escaping? -
x-yuri about 5 yearsFails:
perl -e 'my $f = ".../a b"; print(qx/file $f/);'
. Succeeds:perl -e 'use IPC::Open2; my $f = ".../a b"; open2(my $out, my $in, "file", $f); print(readline($out))'
-
x-yuri about 5 yearsIt'd be great to have an example here. You can use my comment for inspiration.
-
Hi-Angel over 3 yearsIt is worth adding that the command return code is stored at
$?
, in case you want to peek at it before making use of the command output.