in reply to How to know the status of a command invoked by open function?
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:
If those weren’t obvious, this is simpler:$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);
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:$kidpid = open(FROM_KID, "somecmd 3>&1 1>&2 2>&3 3>&- |") // die $!;
Now to use that knowledge in the earlier examples, just throw a fourth ball up into the air.$fd3 = $fd1; $fd1 = $fd2; $fd2 = $fd3; $fd3 = undef;
Easy as pi!
|
|---|