1

I want this statement to keep looping while the value of the json field in this api response is null.

while $(curl --location --request GET "https://example.com/integration-test/results/json"| jq '.result') == null
    do
    echo "Waiting for Integration tests to finish. Trying again in 10 seconds."
    sleep 2
done

The == null obviously doesn't work, but its illustrative of what I'm going for. When the Integration tests finish this api call will instead return SUCCESS or FAILURE in 'result'. Thats when I want the loop to stop.

1
  • What is the output of the wget ... | jq ... command that you want to test for? Is it actually going to be the string "null", or just nothing, or something else? Commented Jan 14, 2021 at 21:37

2 Answers 2

3

Consider using the -e argument to jq to make its exit status reflect its output (emitting a failed exit status when the only output is either null or false).

Writing the code below for clarity rather than terseness (using explicit grouping operators to make it clear that the negation applies to the entire pipeline):

#!/usr/bin/env bash
set -o pipefail  # make a failure on the left-hand side fail the entire pipeline
while ! { curl --fail -L "https://example.com/integration-test/results/json" \
          | jq -e '.result' >/dev/null; }; do
    echo "Waiting for Integration tests to finish. Trying again in 10 seconds."
    sleep 10
done
Sign up to request clarification or add additional context in comments.

Comments

0

You need to put the comparison in a conditional expression.

while [[ "$(curl --location --request GET "https://example.com/integration-test/results/json"| jq '.result')" == null ]]
do
    ...
done

1 Comment

Unfortunately my jq knowledge is very rudimentary. That would be a good alternate answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.