1

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.

10
  • 2
    How exactly do you "send in a 0 or 1"? Commented Mar 10, 2023 at 11:58
  • 1
    Your sample code doesn't print [e2e-st]. What does this represent and where does it come from? Commented Mar 10, 2023 at 11:59
  • 2
    Can you say something about the STATUS variable? You seem to want to test its value for 0, but you also use it as a filename with cat (do you have files called 0 and 1?) The code never assigns a value to the variable. As roaima said, it's also unclear what produces the output that you quote.
    – Kusalananda
    Commented Mar 10, 2023 at 12:10
  • @roaima this from OpenShift Tekton pipeline. - name: STATUS value: $(steps.step-run-script.exitCode.path) script: | #!/usr/bin/env bash set -eo pipefail ... And [e2e-st] is the prompt of my my pipeline terminal.
    – fatkobra
    Commented Mar 10, 2023 at 12:34
  • @Kusalananda The STATUS is defined and we get the value the following way: ``` - name: STATUS value: $(steps.step-run-script.exitCode.path) script: | #!/usr/bin/env bash set -eo pipefail ``` And I am getting the correct execution codes using cat "${STATUS}" just the text assign to the variable RESULT is not working.
    – fatkobra
    Commented Mar 10, 2023 at 12:35

1 Answer 1

0

Without knowing anything about OpenShift or Tekton, the word "path" in "steps.step-run-script.exitCode.path" appears to indicate a filename, as does the fact that cat $STATUS works.

Put something like

echo "STATUS=$STATUS"
ls -l "$STATUS"

in the script to see what the value of the variable $STATUS is, and to see the file.

The contents of the file don't automatically get copied in a variable holding the name of the file, so you need to use e.g. cat to read it, as you do later in the script. E.g.

STATUSVALUE=$(cat "$STATUS")
if [ "$STATUSVALUE" -eq 0 ]; then
    echo ok
else
    echo failed...
fi

If you can, you might also consider renaming the variable, something like STATUSFILE might be more descriptive.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .