Bash while loop with two string conditions

I am having a problem with my bash script. My script, among other things, starts a server, which takes some time. To combat a long run, I set up a while loop that the server asks to see if it works.

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 is equal to the line "404". If so, the thing should still be in a loop, right? My loop seems to be coming out prematurely.

Joe

+4
source share
2 answers

[ .. ] does not match glob. Use [[ .. ]] :

 if [ "$response" == "" ] || [[ "$response" == *"404 Not Found"* ]]; then 
+6
source

I'm not sure why my original script failed. I assume this is due to what I was comparing with HTML. To get the script working, I ended up using string length instead of content. Using the following comparator has everything that runs smoothly.

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

Thanks for the help, Joe.

0
source

Source: https://habr.com/ru/post/1490391/


All Articles