Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

STDOUT STDERR and response code from external Linix command

by gdanenb (Acolyte)
on Jan 04, 2012 at 20:02 UTC ( #946281=perlquestion: print w/ replies, xml ) Need Help??
gdanenb has asked for the wisdom of the Perl Monks concerning the following question:

I want to get both stdout and stderr into $var, and get correct $?
I went through solutions in perlfac8 and forums
The strange thing that I DON'T get sdterr in any case, as long as correct result code.
perl version is 5.8.8, Linux RH 5

my $cmd="lsss"; my $out=`$cmd 2>&1`; or my $out=qx($cmd 2>&1); or open(PIPE, "$cmd 2>&1|");

stdout when command is correct I get ok. I don't want to use additional *capture* modules.

Comment on STDOUT STDERR and response code from external Linix command
Download Code
Replies are listed 'Best First'.
Re: STDOUT STDERR and response code from external Linix command
by zentara (Archbishop) on Jan 05, 2012 at 12:49 UTC
Re: STDOUT STDERR and response code from external Linix command
by mr.nick (Chaplain) on Jan 04, 2012 at 21:21 UTC
    Something like this?
    [a209624@cdrzw2051 ~]$ cat foo.pl my $cmd="lessss"; my $ret=`sh -e '$cmd' 2>&1`; print "$cmd = $ret = $?\n"; [a209624@cdrzw2051 ~]$ perl foo.pl lessss = sh: lessss: No such file or directory = 256

    mr.nick ...

      my $ret=`sh -e '$cmd' 2>&1`;

      Didn't you mean -c ?

      for my $cmd ('date', 'dat', 'date -foo') { print "cmd: $cmd\n"; my $out = `sh -c '$cmd' 2>&1`; my $rc = $? >> 8; print "out: $out"; print "rc: $rc\n\n"; } __END__ cmd: date out: Wed Jan 4 22:40:20 CET 2012 rc: 0 cmd: dat out: sh: dat: not found rc: 127 cmd: date -foo out: date: `oo': No such file or directory rc: 1

        Could you pls explain why I need open one more shell?

        Do I need to do the same when calling external scripts(perl,python) ?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946281]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2015-08-01 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found
    past polls