1

I am using the bash script wait for it to check if a container is up and running before I launch another container.

https://github.com/vishnubob/wait-for-it

It seems that if it's successful it returns 0 and if not 124. Is it possible to check this value and if it's not successful exit the script?

I've tried

./wait-for-it.sh $BROKER_ADDRESS
echo $?
if ($?==124)
    then
    echo "exiting as broker service never became available"
    exit
fi

And I can see it echos 124 in my terminal but then the check fails and it carries on launching the container. I'm assuming my conditional check is wrong but I can't seem to figure out why

4
  • 2
    You need to save $? right after the call to wait-for-it.sh in a variable and use it. Otherwise, $? could be different - it represents the last command's exit status. Commented May 4, 2020 at 15:25
  • if [[ $? -eq 124 ]]; then Commented May 4, 2020 at 15:25
  • 1
    After the echo, $? is 0 because the echo succeeded. Use status=$? and then check $status, etc. Or test $? without echoing it, but capturing it gives you more flexibility. Commented May 4, 2020 at 15:25
  • 1
    An arithmetic conditional requires double parens, like (($? == 124)), by the way. Otherwise, you're trying to run $?==124 as a command in a subshell. Commented May 4, 2020 at 15:29

1 Answer 1

1

$? Always returns the exit status of the previous command so in this case in your 3rd line in if loop you are comparing exit status of the echo command in your second line to 124. So either remove the second line or store the exit status to a variable & use that variable in your if loop.

Sign up to request clarification or add additional context in comments.

Comments

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.