return status for nonexistent command run in backticks

J

Jeff

i quite a few commands via backticks in my perl programs. i have a
routine that i use to check the $? variable for return status/core
dump/signal values for commands that are at risk of core dumping. the
routine looks like:

sub statCheck
{
my ($childError) = @_;
my ($rc, $core, $sig);

return (undef, undef, undef) if (not defined $childError);

$rc = $childError >> 8;
$core = $childError & 128 ? TRUE : FALSE;
$sig = $childError & 127;

return ($rc, $core, $sig);
}

this works fine most of the time. but if i call a command that
doesn't exist, $? = -1 and this logic blows up. i can put a check in
here for -1 easily enough, but my question to you all is should
backticks be returning -1 ever? if i try running a nonexistent
command from the shell prompt (ksh), i get a return status of 127 (as
the man page for ksh says it should).

thoughts?

jeff
 
G

Glenn Jackman

Jeff said:
sub statCheck
{
my ($childError) = @_;
my ($rc, $core, $sig);

return (undef, undef, undef) if (not defined $childError);

$rc = $childError >> 8;
$core = $childError & 128 ? TRUE : FALSE;
$sig = $childError & 127;

return ($rc, $core, $sig);
}

this works fine most of the time. but if i call a command that
doesn't exist, $? = -1 and this logic blows up. i can put a check in
here for -1 easily enough, but my question to you all is should
backticks be returning -1 ever? if i try running a nonexistent
command from the shell prompt (ksh), i get a return status of 127 (as
the man page for ksh says it should).

I don't see where it "blows up". If $childError == -1, then $rc might
be 16777215. What does the caller of statCheck do with its returned
value?

However, you can check for $childError == -1, and do something with $!
which contains the reason for failure.

Or, you can use a mask to only get an 8 bit value for $rc:
$rc = ($childError & 0xff00) >> 8;
which would make $rc == 255 if $childError == -1
 
J

Jeff

Glenn Jackman said:
I don't see where it "blows up". If $childError == -1, then $rc might
be 16777215. What does the caller of statCheck do with its returned
value?

However, you can check for $childError == -1, and do something with $!
which contains the reason for failure.

Or, you can use a mask to only get an 8 bit value for $rc:
$rc = ($childError & 0xff00) >> 8;
which would make $rc == 255 if $childError == -1

i guess my real question is why is the return status = 127 when i type
a nonexistent command at the shell prompt and = -1 when i run it from
a perl program? for now i guess i'll put a check in for -1 and assume
that means the command didn't exist.

jj
 
B

Ben Morrow

i guess my real question is why is the return status = 127 when i type
a nonexistent command at the shell prompt

Because that's what ksh does.
and = -1 when i run it from a perl program?

Because that's what Perl does. perldoc -f system.
for now i guess i'll put a check in for -1 and assume that means
the command didn't exist.

No need to assume, it's documented.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top