1#!/usr/bin/env bash
2
3function retry3 {
4  local n=1
5  local max=3
6  local delay=1
7  while true; do
8    # shellcheck disable=SC2015
9    "$@" && break || {
10      if [[ $n -lt $max ]]; then
11        ((n++))
12        echo "Command failed. Attempt $n/$max:"
13        sleep $delay;
14      else
15        echo "The command has failed after $n attempts." >&2
16        return 1
17      fi
18    }
19  done
20}
21
22if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
23  retry3 "$@"
24fi
25