return status for nonexistent command run in backticks

Discussion in 'Perl Misc' started by Jeff, Feb 9, 2004.

  1. Jeff

    Jeff Guest

    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
    Jeff, Feb 9, 2004
    #1
    1. Advertising

  2. Jeff <> wrote:
    > 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

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Feb 9, 2004
    #2
    1. Advertising

  3. Jeff

    Jeff Guest

    Glenn Jackman <> wrote in message news:<>...
    > Jeff <> wrote:
    > > 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


    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
    Jeff, Feb 10, 2004
    #3
  4. Jeff

    Ben Morrow Guest

    (Jeff) wrote:
    > 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

    --
    And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
    * *
    Ben Morrow, Feb 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    382
    Peter Hansen
    Jul 6, 2004
  2. puzzlecracker
    Replies:
    4
    Views:
    367
    James Kanze
    Sep 15, 2008
  3. Hal Fulton

    fxruby (nonexistent) html widget

    Hal Fulton, Aug 11, 2005, in forum: Ruby
    Replies:
    3
    Views:
    96
    Lothar Scholz
    Aug 11, 2005
  4. Ravi Parimi
    Replies:
    8
    Views:
    113
    Peter Sundstrom
    Jan 26, 2004
  5. Skye Shaw!@#$

    Backticks and exit status on Windows

    Skye Shaw!@#$, Aug 11, 2013, in forum: Ruby
    Replies:
    0
    Views:
    243
    Skye Shaw!@#$
    Aug 11, 2013
Loading...

Share This Page