You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.1 KiB
Bash

5 years ago
#!/bin/sh
5 years ago
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2020-02-21 20:41:52 +0000 (Fri, 21 Feb 2020)
#
2 years ago
# https://github.com/HariSekhon/DevOps-Bash-tools
5 years ago
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
2 years ago
# https://www.linkedin.com/in/HariSekhon
5 years ago
#
5 years ago
# Had to do this in /bin/sh not bash so that it can be used on bootstrapping Alpine builds
5 years ago
set -eu
5 years ago
[ -n "${DEBUG:-}" ] && set -x
tries="${TRIES:-3}"
usage(){
cat <<EOF
usage: ${0##*/} [<num_tries>] <command>
EOF
exit 3
}
for arg; do
case "$arg" in
--help) usage
;;
esac
done
5 years ago
# bash only
#if [[ "${1:-}" =~ ^[[:digit:]]+$ ]]; then
if echo "${1:-}" | grep -Eq '^[[:digit:]]+$'; then
5 years ago
tries="$1"
shift
fi
if [ $# -lt 1 ]; then
usage
fi
5 years ago
set +e
5 years ago
# {1..$tries} doesn't work and `seq` is a needless fork
5 years ago
# bash only
#for ((i=0; i < tries; i++)); do
for _ in $(seq "$tries"); do
5 years ago
eval "$@"
result=$?
if [ $result -eq 0 ]; then
break
fi
done
exit $result