Why is the return value of Perl's system not w

2019-07-14 20:22发布

问题:

Let me begin by explaining what I'm trying to accomplish. Essentially there are two Perl scripts. One is what I call the Main script with an UI. The user who runs this script will see a list of other scripts he can call from the menu. This list is loaded through a custom config file. The purpose of the main script is to be able to add other scripts in the future as needed without changing the source and be run either as cron job (Non-Interactive mode) and as the user needs (Interactive Mode). As company policy, I am not entitle to post the entire script, so I will post the Interactive-Mode user selection section:

for($i = 0;$i < @{$conf}+1;$i++)
    {
        if($i % 2 == 1 || $i == 0)
        {
            next;
        }
        print $n++ . ". @{$conf}[$i-1]\n";
    }
    print "(health_check) ";

    #
    # User selection
    #

    my $in = <>;
    chomp($in);

    if($in =~ /[A-Za-z]/)
    {
        write_log("[*] Invalid Selection: $in");
        print "\n<<<<<<<<<<<<>>>>>>>>>>>>>\n";
        print ">>> Invalid Selection <<<\n";
        print "<<<<<<<<<<<<>>>>>>>>>>>>>\n";
    }
    elsif($in == 0)
    {
        write_log("Exiting interactive mode");
        last;
    }
    elsif(scalar($scripts[$in]))
    {
        write_log("[*] running: $scripts[$in]");
        $rez = system('./' . "$scripts[$in]");

        if($rez == 0b00)
        {
            printf("%s: [OK]\n",$scripts[$in]);
        }
        elsif($rez == 0b01)
        {
            printf("%s: [WARNING]\n",$scripts[$in]);
        }
        elsif($rez == 0b11)
        {
            printf("%s: [NOT OK]\n",$scripts[$in]);
        }
        else
        {
            print "UNKOWN ERROR CODE: $rez\n";
        }
    }
    else
    {
        write_log("[*] Invalid Selection: $in");
        print "\n<<<<<<<<<<<<>>>>>>>>>>>>>\n";
        print ">>> Invalid Selection <<<\n";
        print "<<<<<<<<<<<<>>>>>>>>>>>>>\n";    
    }

    print "\n\nPress return/enter to continue...";
    <>;
}

write_log("Exiting interactive mode");

}

@{$conf} is a reference to the list of available scripts. It has both the name of the scripts and the path to the script.

$i is used for looping.
$n is the script number which is used for the user to select which script to run.
$in is the user input in decimal value to select which script to run.
$scripts is the actual name of the script and not the path to the script.
$rez is the return code from the scripts.

Here is where it gets weird. I have a script that checks for filesystem usage. Once that is checked, it will exit with the appropriate value for the Main script to process.

0 is Ok
1 is Warning
2 is Alert
3 is Warning + Alert

Here is the relevant part of the filesystem check script:

    if(check_hdd($warning_lvl, $alert_lvl))
{
    $return_val = $return_val | 0b01;
}

if(check_hdd($alert_lvl))
{
    $return_val = $return_val | 0b10;
}

exit $return_val;

The check_hdd subroutine will return 1 if anything is in between the range of the two arguments that is put in (e.g., it will return 1 if it detects anything between the range is filesystem usage in percentage with a default of 100% for the second argument).

So this is where it gets weird...

If for example, the hdd script returns 1. The main script will see 256.

So I went in the hdd script and forced it to return 256.

exit 256;

Main script saw: 0. So I did this with various values and built a small table.

HDD_Check Exit Value            Main is seeing Exit Value as
         1                                    256
         256                                    0
         257                                  256
         258                                  512
         259                                  768

Ahh. Intriguing. Lets convert that into Binary.

HDD_Check Exit Value (Base 2)       Main is seeing Exit Value as (Base 2)
         0b0000000001                             0b0100000000
         0b0100000000                             0b0000000000
         0b0100000001                             0b0100000000
         0b0100000010                             0b1000000000
         0b0100000011                             0b1100000000

Weird. Looks like its doing the following while passing the value:

return_value = return_value << 8

So now that the long winded explanation is done, anyone have any idea? I've also tried this with die instead of exitand it does the same. And for some reason I have the impression it's something very obvious which I'm missing...

回答1:

This is the defined behaviour.

http://perldoc.perl.org/functions/system.html

The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below).

Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).