endless loop in until loop for unix script

I am doing a unix script using until loop.The idea of my script is it need to continuously verify the url status whether is up or down.When its up, it will exit the loop. In my script, I will use curl for check the url status and grep the status code which is equal to 200 only . From what I understood how the until loop works, it will keep on execute until the condition is satisfied on we defined. When I test my script, It end up in the endless loop.
Here are my script :

var=""
until [echo "$var" | grep -q "HTTP/1.1 200 OK"]
do 
  var=$(curl -I -k https://mystie url | grep "HTTP/1.1 200 OK")
  echo $var
done

If I test the var=$(curl -I -k https://mystie url | grep "HTTP/1.1 200 OK") directly in the terminal, it was able to return me the correct result.

Asked By: attack

||

Square brackets can only contain conditional expressions (either unary or binary), therefore you need to drop the square brackets to use the exit status of your command as a condition:

until curl -I -k https://mystie url | grep -q "HTTP/1.1 200 OK"
do
   .... do something .... 
done

Since you’re using the square brackets incorrectly, the condition fails (and exits with non-zero status), thus your until loop never stops.

Square brackets ([ ... ]) can only contain conditional expressions and it must have a space after the first and before the last bracket.

E.g. [ "$var" = "expected value" ]
and not ["$var" = "expected value"]

Answered By: annahri
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.