Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid script exit on curl failure

Tags:

bash

shell

curl

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
like image 431
paul Avatar asked Jul 27 '26 09:07

paul


1 Answers

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
like image 67
Stefan Hegny Avatar answered Jul 30 '26 04:07

Stefan Hegny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!