Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Changed a bit in case of wrong or weird input. (The quotes on the right side of assignments aren't necessary but I find it to be a good practice to use quotes everywhere.)

  #!/usr/bin/env bash

  function retry {
      local retries="$1"
      shift
  
      case "$retries" in
          ""|*[!0-9]*)
              echo "retry: First argument must be a number, got '$retries'" 1>&2
              return 1
          ;;
      esac
  
      local count=0
      until "$@"; do
          local exit="$?"
          local wait="$((2 ** count))"
          count="$((count + 1))"
          if [ "$count" -lt "$retries" ]; then
              echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." 1>&2
              sleep "$wait"
          else
              echo "Retry $count/$retries exited $exit, no more retries left." 1>&2
              return "$exit"
          fi
      done
      return 0
  }

  retry "$@"


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: