在以下 bash shell 脚本中,until 循环无法正常工作。
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
输出
+ 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循环不等待状态获得GOOD_HEALTH,我在这里做错了什么?
最佳答案
-eq
运算符用于数字, 而非字符串。对于字符串,您需要 =
。如果您查看 bash
文档,您会看到(我的重点):
arg1 OP arg2
:
OP
is one of-eq
,-ne
,-lt
,-le
,-gt
, or-ge
.
These arithmetic binary operators return true ifarg1
is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal toarg2
, respectively.
Arg1
andarg2
may be positive or negative integers.
例如:
pax:~> [[ 'xx' = 'yy' ]] && echo WTF
pax:~> [[ 'xx' -eq 'yy' ]] && echo WTF
WTF
关于linux - 直到循环不适用于 shell 中的字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63736208/