Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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

if [ "$response" == "" ] || [[ "$response" == *"404 Not Found"* ]]; then
share|improve this answer
    
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. –  Gordon Davisson Jul 9 '13 at 0:13
    
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. –  Joe Andolina Jul 9 '13 at 0:19
    
@JoeAndolina Just a thought - have you verified this with another site that definitely gives a 404? –  Preet Sangha Jul 9 '13 at 0:39
    
@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>... +++, –  Joe Andolina Jul 9 '13 at 0:41
    
@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. –  that other guy Jul 9 '13 at 3:11

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.