我有一个脚本,我在其中检查执行代码(0 分配给SUCCESS和 1 分配给FAILURE)。我正在使用 if-else 语句来检查执行代码是 0 还是 1。
如果它是 0,我想将文本SUCCESS分配给变量 RESULT。否则将文本FAILURE分配给变量 RESULT。
注意:我需要在其他地方使用 RESULT。
这是我的代码:
#!/usr/bin/env bash
set -eo pipefail
RESULT="SUCCESS"
if [ "${STATUS}" != "0" ]; then
RESULT="FAILURE"
else
RESULT="SUCCESS"
fi
cat "${STATUS}"
echo
echo ${RESULT}
但是,看起来无论我发送 0 还是 1 都没有关系,结果总是失败。这是输出:
[e2e-st] 0
[e2e-st]
[e2e-st] FAILURE
我希望获得 0 个SUCCESS,而不是 0 个FAILURE。
关于我做错了什么或我可以做些什么的任何提示?
在此处获得帮助和解决方案后:
- name: STATUS
value: $(steps.step-run-script.exitCode.path)
script: |
#!/usr/bin/env bash
set -eo pipefail
echo "STATUS=$STATUS"
ls -l "$STATUS"
STATUSVALUE=$(cat "$STATUS")
echo ${STATUSVALUE}
if [ "$STATUSVALUE" -eq 0 ]; then
echo ok
RESULT="SUCCESS"
echo ${STATUSVALUE}
echo ${RESULT}
else
echo failed...
RESULT="FAILURE"
echo ${STATUSVALUE}
echo ${RESULT}
fi
这就是我得到的:
[e2e-st : send-test-report-and-status-to-jira-comment] STATUS=/tekton/steps/step-run-script/exitCode
[e2e-st : send-test-report-and-status-to-jira-comment] -rw-r--r--. 1 1000 1002340000 1 Mar 10 14:03 /tekton/steps/step-run-script/exitCode
[e2e-st : send-test-report-and-status-to-jira-comment] 0
[e2e-st : send-test-report-and-status-to-jira-comment] ok
[e2e-st : send-test-report-and-status-to-jira-comment] 0
[e2e-st : send-test-report-and-status-to-jira-comment] SUCCESS
和我的测试失败的另一次运行:
[e2e-st : send-test-report-and-status-to-jira-comment] STATUS=/tekton/steps/step-run-script/exitCode
[e2e-st : send-test-report-and-status-to-jira-comment] -rw-r--r--. 1 1000 1002340000 1 Mar 10 14:05 /tekton/steps/step-run-script/exitCode
[e2e-st : send-test-report-and-status-to-jira-comment] 1
[e2e-st : send-test-report-and-status-to-jira-comment] failed...
[e2e-st : send-test-report-and-status-to-jira-comment] 1
[e2e-st : send-test-report-and-status-to-jira-comment] FAILURE
十分感谢你的帮助!一切都按预期工作。