【问题标题】:Shell Script won't fail in JenkinsShell 脚本在 Jenkins 中不会失败
【发布时间】:2017-07-27 14:09:46
【问题描述】:

我有一个简单的 shell 脚本,我想将其设置为定期 Jenkins 作业而不是 cronjob,以便为经验不足的用户提供可见性和可用性。

这是脚本:

#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt

if [ "grep -sq 'Unable' $outputfile" == "0" ]; then
    echo -e "ERROR MESSAGE FOUND\n"
    exit 1
else
    echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
    exit 0
fi

无论 $outputfile 中是否存在“无法”,我的脚本将始终返回“未找到错误消息”,我做错了什么?

如果未找到“无法”,我还需要我的 Jenkins 工作将其归类为成功(例如,如果脚本返回“0”然后通过,其他一切都失败)

【问题讨论】:

    标签: bash shell if-statement jenkins return-value


    【解决方案1】:

    执行 grep 命令并检查退出状态:

    #!/bin/bash
    outputfile=/opt/jhc/streaming/check_error_output.txt
    grep -sq 'Unable' $outputfile
    if [ "$?" == "0" ]; then
        echo -e "ERROR MESSAGE FOUND\n"
        exit 1
    else
        echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
        exit 0
    fi
    

    【讨论】:

    • 你这个人!我自己也该想到的,我盯着它看太久了,我的脑袋已经没了
    【解决方案2】:

    您正在比较两个不同的字符串。结果总是错误的,即 else 部分被占用。

    另外,无需显式查询状态码。这样做:

    if grep -sq 'Unable' $outputfile
    then
      ....
    else
      ....
    fi
    

    【讨论】:

    • 这是我一开始的方式,除了我的 grep 命令在 '[ ]' 中
    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多