diff options
Diffstat (limited to 'bin/untill')
-rwxr-xr-x | bin/untill | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -4,7 +4,7 @@ set -eu usage() { cat <<-'EOF' Usage: - until [-n SECONDS] -- COMMAND... + until [-m MAX] [-n SECONDS] -- COMMAND... until -h EOF } @@ -15,11 +15,12 @@ help() { Options: -n SECONDS the amount of seconds to sleep between attempts (default: 5) + -m MAX the maximum number of attempts (default: unlimited) -h, --help show this message - Runs COMMAND until it eventually succeeds. Sleep SECONDS - between attempts. + Runs COMMAND until it eventually succeeds, trying atmost MAX + times. Sleep SECONDS between attempts. Examples: @@ -29,10 +30,10 @@ help() { $ until guix home build home.scm - Try to build until a new version is successfull, + Try atmost 10 timex to build until a new version is successfull, waiting 30 seconds between attempts: - $ until -n 30 -- x git pull AND make + $ until -m 10 -n 30 -- x git pull AND make EOF } @@ -53,8 +54,11 @@ for flag in "$@"; do done _SECONDS=5 -while getopts 'n:h' flag; do +while getopts 'm:n:h' flag; do case "$flag" in + m) + MAX="$OPTARG" + ;; n) _SECONDS="$OPTARG" ;; @@ -79,5 +83,9 @@ while true; do if "$@"; then break fi + if [ -n "${MAX:-}" ] && [ "$ATTEMPT" -gt "$MAX" ]; then + printf 'Maximum number of attempts reached: %s.\n' "$MAX" >&2 + exit 1 + fi sleep "$_SECONDS" done |