Skip to content

Instantly share code, notes, and snippets.

@unakatsuo
Created June 15, 2011 09:05
Show Gist options
  • Select an option

  • Save unakatsuo/1026755 to your computer and use it in GitHub Desktop.

Select an option

Save unakatsuo/1026755 to your computer and use it in GitHub Desktop.

Revisions

  1. Masahiro Fujiwara revised this gist Oct 14, 2011. 1 changed file with 24 additions and 7 deletions.
    31 changes: 24 additions & 7 deletions gistfile1.bash
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,34 @@

    # Run 10 times at most.
    # % retry 10 echo "xxx"
    #
    # Run multiple lines of command in here document.
    # % retry 10 <<'_END_'
    # echo 1
    # echo 2
    # _END_
    function retry {
    local retry_max=$1
    typeset retry_max=$1
    shift

    local count=$retry_max
    while [[ $count -gt 0 ]]; do
    $* && break
    typeset cmdlst="" i
    if [[ -t 0 ]]; then
    cmdlst="$*"
    else
    read -u 0 -d '' i
    cmdlst="$i"
    fi

    typeset count=$retry_max
    while [[ "$count" -gt 0 ]]; do
    eval "$cmdlst" && break
    count=$(($count - 1))
    sleep 1
    done

    [[ $count -eq 0 ]] && {
    echo "Retry failed [$retry_max]: ${*}" >&2
    exit 1;
    [[ "$count" -eq 0 ]] && {
    echo "Retry failed [$retry_max]: ${cmdlst}" >&2
    return 1;
    }
    return 0
    }
  2. unakatsuo created this gist Jun 15, 2011.
    17 changes: 17 additions & 0 deletions gistfile1.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    function retry {
    local retry_max=$1
    shift

    local count=$retry_max
    while [[ $count -gt 0 ]]; do
    $* && break
    count=$(($count - 1))
    sleep 1
    done

    [[ $count -eq 0 ]] && {
    echo "Retry failed [$retry_max]: ${*}" >&2
    exit 1;
    }
    return 0
    }