Skip to content

Instantly share code, notes, and snippets.

@pilcrow
Created August 21, 2020 18:54
Show Gist options
  • Save pilcrow/38fd0603cff93a78bd51c3912a8e28c3 to your computer and use it in GitHub Desktop.
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
# 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