From: Tom Christiansen <tchrist@mox.perl.com>
Subject: SRC: capturing stdout and stderr differently
Date: 22 May 1998 14:03:22 GMT
To capture a command's stderr and stdout together:
$output = `cmd 2>&1`; # either with backticks
$pid = open(PH, "cmd 2>&1 |"); # or with an open pipe
while (<PH>) { } # plus a read
To capture a command's stdout but discard its stderr:
$output = `cmd 2>/dev/null`; # either with backticks
$pid = open(PH, "cmd 2>/dev/null |"); # or with an open pipe
while (<PH>) { } # plus a read
To capture a command's stderr and discard its stdout:
$output = `cmd 2>&1 1>/dev/null`; # either with backticks
$pid = open(PH, "cmd 2>&1 1>/dev/null |"); # or with an open pipe
while (<PH>) { } # plus a read
To exchange a command's stdout and stderr in order to capture the stderr
but leave its stdout to come out our old stderr:
$output = `cmd 3>&1 1>&2 2>&3 3>&-`; # either with backticks
$pid = open(PH, "cmd 3>&1 1>&2 2>&3 3>&-|");# or with an open pipe
while (<PH>) { } # plus a read
To read both a command's stdout and its stderr separately, it's easiest
and safest to redirect them separately to files, and then read from
those files when the program is done:
system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr");
--tom
--
"SPARC" is "CRAPS" backwards --Rob Pike