I have a bash script which uses
set -o errexit so that on error the entire script exits at the point of failure.
The script runs a
curl command which sometimes fails to retrieve the intended file - however when this occurs the script doesn't error exit.
I have added a
for loop to
- pause for a few seconds then retry the
falseat the bottom of the for loop to define a default non-zero exit status - if the curl command succeeds - the loop breaks and the exit status of the last command should be zero.
#! /bin/bash set -o errexit # ... for (( i=1; i<5; i++ )) do echo "attempt number: "$i curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim if [ -f ~/.vim/autoload/pathogen.vim ] then echo "file has been retrieved by curl, so breaking now..." break; fi echo "curl'ed file doesn't yet exist, so now will wait 5 seconds and retry" sleep 5 # exit with non-zero status so main script will errexit false done # rest of script .....
The problem is when the
curl command fails, the loop retries the command five times - if all attempts are unsuccessful the for loop finishes and the main script resumes - instead of triggering the
How can I get the entire script to exit if this
curl statement fails?