Created
November 29, 2017 02:08
-
-
Save kazegusuri/e961dae9cd06445da54153371917f296 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| retry() { | |
| local result=0 | |
| local count=1 | |
| while [ $count -le 3 ]; do | |
| [ $result -ne 0 ] && { | |
| echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of 3.${ANSI_RESET}\n" >&2 | |
| } | |
| # ! { } ignores set -e, see https://stackoverflow.com/a/4073372 | |
| ! { "$@"; result=$?; } | |
| [ $result -eq 0 ] && break | |
| count=$(($count + 1)) | |
| sleep 1 | |
| done | |
| [ $count -gt 3 ] && { | |
| echo -e "\n${ANSI_RED}The command \"$@\" failed 3 times.${ANSI_RESET}\n" >&2 | |
| } | |
| return $result | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment