2

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?

satish kumar
  • 45
  • 1
  • 10

1 Answers1

3

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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I was about to type that. The notation is the opposite of the way Perl does it — there, the alphanumeric operator names such as `eq` are for comparing strings and the arithmetic operators such as `==` are for comparing numbers. – Jonathan Leffler Sep 04 '20 at 06:55
  • @paxdiablo : Since we are in bash here, we should emphasize that `==` does not do simple equality comparision, but pattern matching, where the rhs is interpreted as wildcard pattern, unless quoted. Example: With `f='r*'`, `[[ rx == $f ]]` matches, but `[[ rx == "$f" ]]` does not. – user1934428 Sep 04 '20 at 07:04
  • @user1934428, I'm not sure that comes into play here since `'GOOD_HEALTH'` has no wildcards - from memory, wild-card matching only works if the wildcarded thing is on the right of the `=`. In other words, `[ $f = rx ]` also does not. But I'll make an addendum to call this out. – paxdiablo Sep 04 '20 at 07:08
  • In this case, no. I thought it's good to mention it for correctness. Of course if you use the _test_ command (`[ xx = yy ]`), you always have pure string equality. – user1934428 Sep 04 '20 at 07:11
  • Hi @paxdiablo now until loop is going in infinite loop. `+ cm_health_check='"GOOD_HEALTH"' ` `+ [ '"GOOD_HEALTH"' '=' GOOD_HEALTH ] ` – satish kumar Sep 04 '20 at 08:37