user1537766
user1537766

Reputation: 162

How to make a shell script fail the build on unsuccesfull execution?

I have a shell script which executes on a CI server , the shell script does a couplr of Curl's . Now i would want the script to fail if the curl is unsuccessfull, I read that "$?" will give the status of the statement executed , how do i use this to achieve what i want ?

function do_curl 
    {
      echo "==== Posting $file_name to DHIS ===="
      curl -H "Content-Type: application/json" -u $authorization -d @$file_name $url -X POST || exit
      echo ""
    }

this function is called repeatedly , i want it to exit even when it fails the first time , right now , it does not exit , but executes the number of times its called , even though it fails the very first time.

Upvotes: 0

Views: 231

Answers (2)

griffin
griffin

Reputation: 1258

Update:

For a possible answer to your question using functions (the original question did not have a function), see here: Is there a way to write a bash function which aborts the whole execution, no matter how it is called?

(you can also update your question further to get better answers)


One possible solution would be using something like this for every line:

curl ... || exit

Of course, if you have multiple urls, you can encapsulte that in a function for example, or you could chain them this way:

curl ... && curl ... && ...

You can also break the current line using \ like this:

curl ... && \
curl ... && \
...

This also works because a script without explicit exit statement should return the last set exit code, e.g. the one returned by the last executed curl command in this case.


Regarding the used operators:

a || b 

Means: evaluate a OR b, and the || is logically applied to the return code of a, so if a returns 0 or something not 0, b will be executed anyway.

a && b

Means: execute a AND b, and the && is again logically applied to the return code of a, so if a returns 0 (=false), b won't be executed as the evaluation will stop at that point, but if a returns a value not 0 (=true), evaluation and thus execution will continue.

You can additionally combine this with subshells using parantheses (( and )) and other stuff to build some quite complex command chains, but I would advise to follow the KISS ("Keep it simple, stupid!") principle in case of shell scripts and better write everything on their own line, additionally commenting ( using # ), so you still know what the script does if you have to look at it a year later ;)

While these things have been the same on all the shells I've used so far, there can still be differences, but if you, like me, have bash on most machines, this might be interesting to you:

Bash Reference Manual

Upvotes: 2

Charles Duffy
Charles Duffy

Reputation: 295678

curl --fail ... || exit

...will immediately exit the shell script with curl's exit status in the event that said curl call fails.

It's also possible to get exit-on-failure behavior globally with set -e, but this should only done by experts; see http://mywiki.wooledge.org/BashFAQ/105 for a discussion of its limitations and pitfalls.

Upvotes: 2

Related Questions