Shell脚本以轮询服务是否启动

时间:2019-07-10 22:05:34

标签: bash shell

我想知道代码中是否有错误

while true
do
    var=$(curl http://${SERVER_HOST}:8060/vmc/vdisizer/api/v1/health) && echo "Got Reply from $SERVER_HOST" || echo "Curl failed to connect to $SERVER_HOST" 
    if [ "$var" -eq "OK" ]
    then
        echo "Success"
        break
    fi
done

这不起作用,并且出现以下错误-

int-tester_1     | curl: (52) Empty reply from server

我希望它不断轮询服务,直到服务回复“确定”,然后脱离循环。

将-eq更改为=时,出现以下错误-

int-tester_1     | + var=OK
int-tester_1     | Got Reply from 10.2.223.87
int-tester_1     | + echo 'Got Reply from 10.2.223.87'
int-tester_1     | + '[' = OK ']'
int-tester_1     | ./run_itests.sh: line 33: [: =: unary operator expected

1 个答案:

答案 0 :(得分:1)

问题可能与以下命令有关 是否正在填充SERVER_HOST

curl http://${SERVER_HOST}:8060/vmc/vdisizer/api/v1/health

也 你可以

(var=$(curl http://${SERVER_HOST}:8060/vmc/vdisizer/api/v1/health)) && echo "Got Reply from $SERVER_HOST" || echo "Curl failed to connect to $SERVER_HOST" 

是否curl http://10.2.223.87:8060/vmc/vdisizer/api/v1/health 得到输出?

类似于Curl Error 52 Empty reply from server