exit code - Why is the return value of Perl's system not what I expect? -
let me begin explaining i'm trying accomplish. there 2 perl scripts. 1 call main script ui. user runs script see list of other scripts can call menu. list loaded through custom config file. purpose of main script able add other scripts in future needed without changing source , run either cron job (non-interactive mode) , user needs (interactive mode). company policy, not entitle post entire script, post 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 # $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 continue..."; <>; } write_log("exiting interactive mode");
}
@{$conf} reference list of available scripts. has both name of scripts , path script.
$i used looping. $n script number used user select script run. $in user input in decimal value select script run. $scripts actual name of script , not path script. $rez return code scripts.
here gets weird. have script checks filesystem usage. once checked, exit appropriate value main script process.
0 ok 1 warning 2 alert 3 warning + alert
here relevant part of 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 return 1 if in between range of 2 arguments put in (e.g., return 1 if detects between range filesystem usage in percentage default of 100% second argument).
so gets weird...
if example, hdd script returns 1. main script see 256.
so went in hdd script , forced return 256.
exit 256;
main script saw: 0. did various values , built small table.
hdd_check exit value main seeing exit value 1 256 256 0 257 256 258 512 259 768
ahh. intriguing. lets convert binary.
hdd_check exit value (base 2) main seeing exit value (base 2) 0b0000000001 0b0100000000 0b0100000000 0b0000000000 0b0100000001 0b0100000000 0b0100000010 0b1000000000 0b0100000011 0b1100000000
weird. looks doing following while passing value:
return_value = return_value << 8
so long winded explanation done, have idea? i've tried die
instead of exit
and same. , reason have impression it's obvious i'm missing...
this defined behaviour.
http://perldoc.perl.org/functions/system.html
the return value exit status of program returned wait call. actual exit value, shift right 8 (see below).
return value of -1 indicates failure start program or error of wait(2) system call (inspect $! reason).
Comments
Post a Comment