paul paul - 6 months ago 10
Bash Question

Avoid script exit on curl failure

I´m using to wait until a service is up and runnig, but using curl after the first attemp to connect and fail the whole script exit.
How can avoid the exit after the fail of the curl?. I´ve tried using --fail on curl but nothing.

Here my code

#!/bin/bash

getX(){
echo `curl --fail domain.com` --> should return an array
}
test(){
result=`getX`
while [ ${#result[@]} -eq 0 ]
do
echo "waiting"
result=`getX`
done
}
test

Answer

My suggestion would be not to use the output, but the return code of curl, how about that?

getX(){
      result=`curl --fail domain.com` --> should return an array
    rc=$?

 }
 test(){
  getX
  while [ $rc  -ne 0 ]
     do
       echo "waiting"
       getX
  done
 }
 test