gpt4 book ai didi

function - 从函数返回值(从现有变量名称设置变量)

转载 作者:行者123 更新时间:2023-12-02 23:15:57 25 4
gpt4 key购买 nike

我想将两个参数传递给我的函数,并根据结果设置一个变量(和变量名)。我一直试图通过以下方式做到这一点:

$service_to_check_for = "ping"

function check-service-monitored ($check_command,$check_name) {
if($service_to_check_for -eq $check_command)
{
$Value = "Yes"
}
else
{
$Value = "No"
}

$script:check_command = $check_command
$script:check_name = $check_name
$script:value = $Value
}

check-service-monitored "ping" "NagiosPing"

echo "$check_name : $value"

我想要
     $NagiosPing = "Yes"

但是如何?

最佳答案

只要确保函数输出的唯一值就是结果:

$service_to_check_for = "ping"

function check-service-monitored ($check_command,$check_name) {
if($service_to_check_for -eq $check_command)
{
"Yes"
}
else
{
"No"
}

$script:check_command = $check_command
$script:check_name = $check_name
$script:value = $Value
}

$NagiosPing = check-service-monitored "ping" "NagiosPing"
$NagiosPing

您的函数现在提供的唯一输出是Yes或No,只需将其分配给变量

关于function - 从函数返回值(从现有变量名称设置变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9361931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com