让我首先解释我要完成的工作。基本上有两个 Perl 脚本。一个是我称之为带有 UI 的主脚本。运行此脚本的用户将看到他可以从菜单中调用的其他脚本的列表。此列表通过自定义配置文件加载。主脚本的目的是能够在将来根据需要添加其他脚本,而无需更改源代码,并且可以作为 cron 作业(非交互模式)和用户需要(交互模式)运行。作为公司政策,我无权发布整个脚本,所以我将发布交互模式用户选择部分:
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} 是对可用脚本列表的引用。它既有脚本的名称,也有脚本的路径。
$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.
这就是奇怪的地方。我有一个检查文件系统使用情况的脚本。一旦选中,它将以适当的值退出,以便主脚本处理。
0 is Ok
1 is Warning
2 is Alert
3 is Warning + Alert
这是文件系统检查脚本的相关部分:
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;
check_hdd 子例程将返回 1,如果有任何东西在放入的两个参数的范围之间(例如,如果它检测到范围之间的任何东西,它将返回 1 是文件系统使用百分比,第二个参数默认为 100% )。
所以这就是它变得奇怪的地方......
例如,如果 hdd 脚本返回 1。主脚本将看到 256。
所以我进入了 hdd 脚本并强制它返回 256。
exit 256;
主脚本看到:0。所以我用不同的值做了这个并建立了一个小表。
HDD_Check Exit Value Main is seeing Exit Value as
1 256
256 0
257 256
258 512
259 768
啊。耐人寻味。让我们将其转换为二进制。
HDD_Check Exit Value (Base 2) Main is seeing Exit Value as (Base 2)
0b0000000001 0b0100000000
0b0100000000 0b0000000000
0b0100000001 0b0100000000
0b0100000010 0b1000000000
0b0100000011 0b1100000000
奇怪的。看起来它在传递值时执行以下操作:
return_value = return_value << 8
那么现在冗长的解释已经完成,有人知道吗?我也试过用die
代替,exit
它也是一样的。出于某种原因,我的印象是我错过了一些非常明显的东西......