From df9f858ea1cfdf2242aba754ad280b6fb0c257d9 Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Thu, 31 Aug 2023 12:59:21 +0000 Subject: [PATCH 1/2] fix ShellCheck warnings --- retry | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/retry b/retry index c396c5c..6a936e6 100755 --- a/retry +++ b/retry @@ -4,11 +4,12 @@ GETOPT_BIN=$IN_GETOPT_BIN GETOPT_BIN=${GETOPT_BIN:-getopt} __sleep_amount() { - if [ -n "$constant_sleep" ]; then + if [ -n "$constant_sleep" ]; then sleep_time=$constant_sleep else #TODO: check if user would rather use one of the other possible dependencies: python, ruby, bc, dc - sleep_time=`awk "BEGIN {t = $min_sleep * $(( (1<<($attempts -1)) )); print (t > $max_sleep ? $max_sleep : t)}"` + # shellcheck disable=SC2004 + sleep_time=$(awk "BEGIN {t = $min_sleep * $(( (1<<($attempts -1)) )); print (t > $max_sleep ? $max_sleep : t)}") fi } @@ -37,10 +38,10 @@ retry() while [[ $return_code -ne 0 && $attempts -le $max_tries ]]; do - if [ $attempts -gt 0 ]; then + if [[ $attempts -gt 0 ]]; then __sleep_amount __log_out "Before retry #$attempts: sleeping $sleep_time seconds" - sleep $sleep_time + sleep "$sleep_time" fi P="$1" @@ -54,14 +55,14 @@ retry() # command not found exit $return_code elif [ $return_code -ne 0 ]; then - attempts=$[$attempts +1] + attempts=$((attempts +1)) fi done - if [ $attempts -gt $max_tries ]; then + if [[ $attempts -gt $max_tries ]]; then if [ -n "$fail_script" ]; then __log_out "Retries exhausted, running fail script" - eval $fail_script + eval "$fail_script" else __log_out "Retries exhausted" fi @@ -71,11 +72,13 @@ retry() } # If we're being sourced, don't worry about such things -if [ "$BASH_SOURCE" == "$0" ]; then +if [ "${BASH_SOURCE[0]}" == "$0" ]; then # Prints the help text help() { - local retry=$(basename $0) + local retry + retry=$(basename "$0") + cat < Date: Thu, 31 Aug 2023 12:59:37 +0000 Subject: [PATCH 2/2] add tests --- tests | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 tests diff --git a/tests b/tests new file mode 100755 index 0000000..48c47f9 --- /dev/null +++ b/tests @@ -0,0 +1,24 @@ +#! /usr/bin/env bash + +failed= + +./retry 'echo hello' \ +| grep -q hello \ +|| failed=' 1-command-runs' + +./retry -t 3 false 2>&1 \ +| tr '\n' ! \ +| grep -q 'Before retry #3: sleeping [0-9.]* seconds!Retries exhausted!$' \ +|| failed="$failed 2-retrying-works" + +output=$(./retry -t 1 -f 'echo "mission failed"; exit 99' false 2>&1) +status=$? + +echo "$output!$status" \ +| tr '\n' ! \ +| grep -q 'mission failed!99!$' \ +|| failed="$failed 3-fail-script-runs" + +[[ $failed = "" ]] && exit 0 +echo "tests failed:$failed" +exit 1