How to stop shell script if curl failed

4

1

I have script that used curl when i pass wrong parameters to script curl failed but script continue executing. I have tried use curl -f/--fail parameter but problem does not solved. What is the best way to stop script?

I have founded my mistake. I used curl command into another command

echo `curl --fail ... || exit 1`

After removing echo command curl become working properly. Thank you for answer, it is also useful.

Steelflax

Posted 2014-04-16T11:39:46.533

Reputation: 43

2

check for curl's exit code, also duplicate of http://stackoverflow.com/questions/3822621/how-to-exit-if-a-command-failed

– Jasper – 2014-04-16T11:44:04.057

Answers

7

You can check for exit code using $?:

exit_status = $?
if [ $exit_status != 0 ]
  then
    exit $exit_status
fi

If you want to analyze exit status, take a look at the Exit codes section from man curl page. There are a lot of different codes, depending on why it failed.

EDIT : You can use command1 || command2 as well. command2 is executed if and only if command1 has failed:

curl .... || exit 1

ssssteffff

Posted 2014-04-16T11:39:46.533

Reputation: 1 809

2

Just exit if curl ends with a non-zero exit code:

curl http://www.example.com || exit 1

Or, make your script exit on error:

set -e
curl http://www.example.com

choroba

Posted 2014-04-16T11:39:46.533

Reputation: 14 741