I have a script in which I check the execution codes (0 assign to SUCCESS and 1 assign to FAILURE). I am using a if-else statement to check whether the execution code is 0 or 1.
If it is 0 I want to assign the text SUCCESS to a variable RESULT. else assign the text FAILURE to a variable RESULT.
Note: I need to use RESULT somewhere else.
Here is my code:
#!/usr/bin/env bash
set -eo pipefail
RESULT="SUCCESS"
if [ "${STATUS}" != "0" ]; then
RESULT="FAILURE"
else
RESULT="SUCCESS"
fi
cat "${STATUS}"
echo
echo ${RESULT}
However, it looks like doesn't matter what whether I send in a 0 or 1 I keep getting FAILURE for result. Here is the output:
[e2e-st] 0
[e2e-st]
[e2e-st] FAILURE
I expect to get 0 SUCCESS, instead I get 0 FAILURE.
Any tips on what am I doing wrong or what I could do differently?
After getting help and solution here:
- 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
And this is what I get:
[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
and another run where my tests fail:
[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
Thank you so much for the help! Everything works as expected.
[e2e-st]. What does this represent and where does it come from?STATUSvariable? You seem to want to test its value for0, but you also use it as a filename withcat(do you have files called0and1?) The code never assigns a value to the variable. As roaima said, it's also unclear what produces the output that you quote.