3

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
5
  • 1
    with set -e, the script will exit immediately if a pipeline returns a non-zero status. gnu.org/software/bash/manual/html_node/The-Set-Builtin.html Commented May 20, 2016 at 12:58
  • --fail doesn't prevent a non-zero status, it just prevents output on HTTP errors. Commented May 20, 2016 at 12:59
  • you´re right but before it was not there and was and it´s failing. I update the script on the question Commented May 20, 2016 at 12:59
  • curl does not return an array. It may be returning a JSON list, but that is not the same as a bash array. Commented May 20, 2016 at 13:05
  • I don't know how to answer, there's so much wrong things in this script : 1)curl won't naturally return a bash array ; 2) you should remove the echo from getX, it only adds a linefeed to the result of the curl call 3) unless you've cast the result from curl, you should use string operations on $result, not array operations 4) if you're trying to replay a curl call that returned an HTTP error, you should test curl's return code rather than returned content Commented May 20, 2016 at 13:15

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

why does setting rc to the return code prevent this script from exiting?
because the while loop will run as long as rc is not equal to zero, which is the case when curl runs on error
0

This will never result in an array with zero elements. Use while [ -z "${result[0]}" ] instead. Additionally, replace curl's --fail with -s.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.