user1758367
user1758367

Reputation: 195

Capturing STDERR

I'm trying to capture only STDERR when running this command

my @output = `cleartool setview -exec "cd $myPath; $myBuildCommand" $myView 1>/dev/null`;

However, I always get both STDERR and STDOUT in @output.

I'm trying to catch the warnings and errors from running $myBuildCommand. Any ides?

Upvotes: 0

Views: 416

Answers (3)

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

To capture a command's STDERR but discard its STDOUT (ordering is important here):

 my @output = `cmd 2>&1 1>/dev/null`;

See also How can I capture STDERR from an external command? in perlfaq8.

Upvotes: 3

LeoNerd
LeoNerd

Reputation: 8532

If you want to do any sort of non-trivial command capturing you almost certainly wanted IPC::Run.

use IPC::Run 'run';

my $exitcode = run [ $command, @args ], '>', \my $stdout, '2>', \my $stderr;

At this point, the two scalars $stdout and $stderr will now contain whatever the program wrote to STDOUT and STDERR respectively.

Upvotes: 3

user507077
user507077

Reputation:

Redirect STDERR to STDOUT before redirecting STDOUT to /dev/null. Order is important!

my $stderr = `some-command 2>&1 > /dev/null`;

If you reverse the order of redirections then both STDERR and STDOUT end up in /dev/null.

Upvotes: 3

Related Questions