Created
August 21, 2020 18:54
-
-
Save pilcrow/38fd0603cff93a78bd51c3912a8e28c3 to your computer and use it in GitHub Desktop.
upto.sh – POSIX sh function to repeat a failing command up to N times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Public Domain | |
# Written 2020-08-21, Mike Pomraning | |
# | |
# upto N command [args...] | |
# | |
# Repeatedly execute command until successful, up to N times, returning the | |
# exit status of the last invocation of command. | |
# | |
upto () { | |
NumAttempts=$1; | |
shift; | |
RetVal=0; | |
while [ $NumAttempts -gt 0 ]; do | |
"$@"; | |
RetVal=$?; | |
[ $RetVal -eq 0 ] && break; | |
NumAttempts=$(( $NumAttempts - 1 )); | |
done | |
return $RetVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment