6

I have run into a bit of a problem with my bash script. My script among other things is starting a server that takes some time to get going. In order to combat the long startup, I have put in a while loop that queries the server to see if its running yet.

while [ $running -eq 0 ]; do
echo "===" $response "===";
if [ "$response" == "" ] || [ "$response" == *"404 Not Found"* ]; then
    sleep 1;
    response=$(curl $ip:4502/libs/granite/core/content/login.html);
else
   running=1;
fi
done

When exiting the loop $response equals the "404" string. If thats the case, the thing should still be in the loop, shouldn't it? Seems my loop is exiting prematurely.

Joe

2 Answers 2

6

[ .. ] doesn't match by glob. Use [[ .. ]]:

if [ "$response" == "" ] || [[ "$response" == *"404 Not Found"* ]]; then
Sign up to request clarification or add additional context in comments.

6 Comments

Note that [[ ]] is a bash extension, so make sure the script starts with #!/bin/bash, not #!/bin/sh. There are ways to do an equivalent test in plain POSIX shell syntax, but they aren't nearly as clean.
Thank you for the answer, but even with your changes, I am still seeing the loop exiting while response has the 404 string in it.
@JoeAndolina Just a thought - have you verified this with another site that definitely gives a 404?
@Preet, My output in the loop is surrounded with === and my output after the loop is surrounded with +++, === <html><head><title> 404 Not Found </title>... ===, - after - +++ <html><head><title> 404 Not Found </title>... +++,
@JoeAndolina Your echo statement destroyed whitespace, so if the text contained e.g. two spaces or a no-break space, it would print the same but not match. echo "$response" > file with quotes would let you investigate.
|
0

I am not sure why my original script was failing. I assume it has to do with the fact that I was comparing against HTML. To get my script working I wound up using the string length instead of the content. The using the following comparator has everything working swimmingly.

if [ -z "$response" ] || [ ${#response} -lt 100 ]; then

Thanks for the help, Joe

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.