为什么Perl的系统的返回值不是我所期望的?

发布于 2024-09-26 16:09:09 字数 3971 浏览 1 评论 0原文

让我首先解释一下我想要实现的目标。本质上有两个 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 尝试过,效果相同。出于某种原因,我觉得这是一些非常明显的东西,但我错过了......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-10-03 16:09:09

这是定义的行为。

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

返回值是退出状态
等待返回的程序
称呼。要获得实际的退出值,
右移八位(见下文)。

返回值-1表示程序启动失败或wait(2)系统调用错误(检查$!以了解原因)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文