0

I wanted to know if there is any error in the code

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

This does not work and I get the following error -

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

I want this to continuously poll the service till the service replies "OK" and then break from the loop.

On changing -eq to = , I get the following error -

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
vmwgeek
  • 87
  • 2
  • 11
  • 1
    It's not causing the current problem, but `-eq` does integer comparison, not string comparison. Inside a `[ ]` test, use `=` for string comparison. Also, be aware that web content generally use CRLF line endings, so if the reply is nominally `OK`, it'll actually be `OK\r\n` (where `\r` represents carriage return and `\n` represents newline); the `$()` will trim the `\n` but leave the `\r`, and the carriage return will cause the two strings to be unequal. – Gordon Davisson Jul 10 '19 at 22:49
  • Got the following error - ` 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 ` – vmwgeek Jul 10 '19 at 22:53
  • Made the change – vmwgeek Jul 10 '19 at 22:58
  • The updated test isn't actually getting the `$var` variable -- what exactly is that line now? Also, what's that `echo`? – Gordon Davisson Jul 10 '19 at 23:02
  • 2
    In general, [shellcheck.net](https://www.shellcheck.net/) is the best place to start when debugging bash scripts. – jeremysprofile Jul 10 '19 at 23:04
  • Line 33 is if [ "$var" -eq "OK" ] – vmwgeek Jul 10 '19 at 23:10
  • echo is from the answer below – vmwgeek Jul 10 '19 at 23:10
  • 1
    Oh, that explains it. When you put the `var=` part inside `( )`, it runs in a subshell and the assignment to `var applies only in that subshell. Remove the parentheses. – Gordon Davisson Jul 10 '19 at 23:22
  • I updated it in the question, is that correct? – vmwgeek Jul 10 '19 at 23:26
  • Oh that worked, thanks a lot! – vmwgeek Jul 10 '19 at 23:28

1 Answers1

1

Issue could be with following command Is SERVER_HOST getting populated

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

also can you have

(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" 

does curl http://10.2.223.87:8060/vmc/vdisizer/api/v1/health get output ?

something similar to Curl Error 52 Empty reply from server

Chakra
  • 66
  • 1
  • 7
  • Yes SERVER_HOST is getting populated. On the terminal I can see curl http://10.2.223.87:8060/vmc/vdisizer/api/v1/health – vmwgeek Jul 10 '19 at 22:43