Just depends which command you want the exit status of. It’s quite easy with just one command, as others have shown you, and it’s only slightly trickier with getting the exit status from a command in a pipeline that isn’t the last such command.

For example, this runs dd to get some output while filtering out some stuff you don’t want with egrep, yet still manages to give you the dd exit status instead of the egrep one:

$dd_command = <<'EO_COMMAND'; device=/dev/rmt8 dd_noise='^[0-9]+\+[0-9]+ records (in|out)$' exec 3>&1 status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) | egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1` exit $status; EO_COMMAND @dd_output = `$dd_command`; $status = ($? >> 8); $signo = ($? & 127); $cored = ($? & 128);

Of course the problem there is you can’t read as you go, so to do that via a pipe, do this:

$dd_command = <<'EO_COMMAND'; device=/dev/rmt8 dd_noise='^[0-9]+\+[0-9]+ records (in|out)$' exec 3>&1 status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) | egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1` exit $status; EO_COMMAND $kidpid = open(DD_PIPE, "$dd_command |") // die $!; while (<DD_PIPE>) { # blah blah blah; } close(DD_PIPE); $status = ($? >> 8); $signo = ($? & 127); $cored = ($? & 128);
If those weren’t obvious, this is simpler:
$kidpid = open(FROM_KID, "somecmd 3>&1 1>&2 2>&3 3>&- |") // die $!;
The status//signo/cored bits will be the same, but this lets you concentrate on your three‐ball juggle. The short story is that’s more clearly read as:
$fd3 = $fd1; $fd1 = $fd2; $fd2 = $fd3; $fd3 = undef;
Now to use that knowledge in the earlier examples, just throw a fourth ball up into the air.

Easy as pi!

//GO.SYSIN DD *, DOODAH, DOODAH


In reply to Re: How to know the status of a command invoked by open function? by tchrist
in thread How to know the status of a command invoked by open function? by zhujian0805

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.