如何根据长期运行命令结果退出或重试

发布于 2025-02-13 00:54:00 字数 1323 浏览 3 评论 0原文

我正在尝试监视long_running_command函数的输出(如果输出包含result ='good'',然后print wow wow < /代码>并返回。但是,直到long_running_command没有返回result ='good'继续尝试,直到达到max_try到目前为止一切都很好。但是,如果命令已经返回result ='失败',则没有重试的点,我如何出错(打印nay)不重试?

以下是示例程序(最小工作问题声明):

#!/bin/bash

MAX_TRY=5


#Simulating a fake long running command that may return good or failed randomly with a small delay
long_running_command()
{

    wait_time=$(shuf -i 1-5 -n 1)
    sleep "$wait_time"

    if [ "$path" -eq 1 ];then
        echo "result='good'"
    elif [ "$path" -eq 0 ];then
        echo "result='failed'"
    else
        echo "This path should never hit"
    fi

}



# Some external conditions cannot be controlled by me, faking it
#0 means fail
#1 means pass
path=$(shuf -i 0-1 -n 1)



check_result() {
    while [ $MAX_TRY -le 10 ];do
    if long_running_command |grep -qE 'result.*good' ;then
        echo "$(date): WOW"
        return 0 
    fi
        echo "$(date): Retrying...so far the output does not have good in it or perhaps its failed already"
    MAX_TRY=$((MAX_TRY + 1))
         
    done
    echo "$(date): NAY"
}

check_result

TLDR,如果命令返回失败或良好,我需要从重试循环中退出。稍后,根据结果打印`哇或否。

I am trying to monitor the output of long_running_command function(can't make changes to this), If the output contains result='good' then print WOW and return. However, until the long_running_command did not return result='good' keep trying until MAX_TRY is reached. So far its all good. However, if the command already returned result='failed' then there is no point of retrying, how can I error out(print NAY) without retrying ?

Below is sample program(Minimum working problem statement):

#!/bin/bash

MAX_TRY=5


#Simulating a fake long running command that may return good or failed randomly with a small delay
long_running_command()
{

    wait_time=$(shuf -i 1-5 -n 1)
    sleep "$wait_time"

    if [ "$path" -eq 1 ];then
        echo "result='good'"
    elif [ "$path" -eq 0 ];then
        echo "result='failed'"
    else
        echo "This path should never hit"
    fi

}



# Some external conditions cannot be controlled by me, faking it
#0 means fail
#1 means pass
path=$(shuf -i 0-1 -n 1)



check_result() {
    while [ $MAX_TRY -le 10 ];do
    if long_running_command |grep -qE 'result.*good' ;then
        echo "$(date): WOW"
        return 0 
    fi
        echo "$(date): Retrying...so far the output does not have good in it or perhaps its failed already"
    MAX_TRY=$((MAX_TRY + 1))
         
    done
    echo "$(date): NAY"
}

check_result

TLDR, I need to exit from the retry loop if the command has returned failed or good. Later, print `WOW or NAY based on the results.

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

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

发布评论

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

评论(1

许久 2025-02-20 00:54:02

将输出保存在变量中。然后,您可以测试它是否包含result ='好'result ='失败'。如果这两个,你都会重试。

check_result() {
    while [ $MAX_TRY -le 10 ]
    do
        output=$(long_running_command | grep -E "result='(good|failed)'")
        case "$output" in
            *good*) 
                # Return success immediately
                echo "$(date): WOW"
                return 0
                ;;
            *failed*)
                # No point in retrying
                break
                ;;
        esac
        MAX_TRY=$((MAX_TRY + 1))
    done
    echo "$(date): NAY"
    return 1
}

Save the output in a variable. Then you can test whether it contains result='good' or result='failed'. If neither, you retry.

check_result() {
    while [ $MAX_TRY -le 10 ]
    do
        output=$(long_running_command | grep -E "result='(good|failed)'")
        case "$output" in
            *good*) 
                # Return success immediately
                echo "$(date): WOW"
                return 0
                ;;
            *failed*)
                # No point in retrying
                break
                ;;
        esac
        MAX_TRY=$((MAX_TRY + 1))
    done
    echo "$(date): NAY"
    return 1
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文