5

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.

9

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
| improve this answer | |
3

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.