until loop is not working with string comparison in shell

Viewed 1053

In the following bash shell script until loop is not working properly.

cm_health_check=$(curl -X GET -u "admin:admin" http://$HOSTNAME:7180/api/v11/cm/service/ | jq '
.entityStatus')
echo $cm_health_check
until [ $cm_health_check -eq "GOOD_HEALTH" ]
do
   cm_health_check=$(curl -X GET -u "admin:admin" http://$HOSTNAME:7180/api/v11/cm/service/ | jq '
.entityStatus')
done

output

 + curl -X GET -u admin:admin http://abcd.xyz.com:7180/api/v11/cm/service/
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
 
    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
    0   503    0     0  17023      0 --:--:-- --:--:-- --:--:-- 17344
 "STARTING"
 + echo '"STARTING"'
 + [ '"STARTING"' -eq GOOD_HEALTH ]

until loop is not waiting for status to get GOOD_HEALTH , what am i doing wrong here?

1 Answers

The -eq operator is for numerics, not strings. For strings, you'd want =. If you look into the bash documentation, you'll see (my emphasis):

arg1 OP arg2:

OP is one of -eq, -ne, -lt, -le, -gt, or -ge.

These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively.
Arg1 and arg2 may be positive or negative integers.

For example:

pax:~> [[ 'xx' = 'yy' ]] && echo WTF
pax:~> [[ 'xx' -eq 'yy' ]] && echo WTF
WTF
Related