linux - 直到循环不适用于 shell 中的字符串比较

标签 linux bash shell unix

在以下 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 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.

例如:

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

关于linux - 直到循环不适用于 shell 中的字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63736208/

相关文章:

linux - grep 彩色线条

shell - k8s:创建部署时使用参数化的镜像标记

bash - 如何在 bash 中使用 if not 条件打印文件

windows - IIS 上等效的 Linux shell

linux - 许可 - Linux 内核

linux - 清除大型 Apache 域日志

python - 如何在每行之前打印带有文件名的文件内容?

bash - 源 shell 脚本到 makefile

linux - 对于硬链接(hard link),为什么文件必须在相同的 linux 文件系统上?

java - 使用 java Runtime.exec() 执行 yum 安装程序并捕获其下载进度