Why is the return value of the Perl system not what I expect?

Let me start by explaining what I'm trying to accomplish. Essentially, there are two Perl scripts. One of them I name Main script with the user interface. The user who runs this script will see a list of other scripts that he can call from the menu. This list is loaded through a custom configuration file. The purpose of the main script is the ability to add other scripts in the future as needed without changing the source and run either as a cron task (non-interactive mode) or in accordance with the needs of the user (interactive mode). As a company policy, I cannot publish the entire script, so I will post the user selection section interactively:

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} - link 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. 

That's where it is weird. I have a script that checks the use of the file system. Once this is noted, 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 file system 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 routine will return 1 if something is between the range of two arguments that are placed (for example, it will return 1 if it finds something between the range - use the file system in percent with a default value of 100% for the second argument).

So that's where it is weird ...

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

So, I went to the hdd script and made it return 256.

 exit 256; 

Main script saw: 0. Therefore, I did this with different 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. Let's convert this to a binary file.

 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 It looks like it does the following when passing the value:

 return_value = return_value << 8 

So now that a long explanation has been made, who has an idea? I also tried this with die instead of exit , and it does the same. And for some reason I got the impression that it is very obvious that I am missing ...

+2
source share
1 answer

This is a specific behavior.

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

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

A return value of -1 indicates a failure to start the program or a wait (2) system call error (check $! For this reason).

+10
source