mirror of
https://github.com/thegeeklab/retry.git
synced 2024-11-21 23:20:40 +00:00
fixes some posix compatibility issues
This commit is contained in:
parent
19ef4b5579
commit
8c440f34aa
33
retry
33
retry
@ -1,5 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
#!/usr/bin/env sh
|
||||
GETOPT_BIN=$IN_GETOPT_BIN
|
||||
GETOPT_BIN=${GETOPT_BIN:-getopt}
|
||||
|
||||
@ -9,7 +8,7 @@ __sleep_amount() {
|
||||
else
|
||||
#TODO: check for awk
|
||||
#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)}"`
|
||||
sleep_time=$(awk "BEGIN {t = $min_sleep * $(( (1<<(attempts -1)) )); print (t > $max_sleep ? $max_sleep : t)}")
|
||||
fi
|
||||
}
|
||||
|
||||
@ -37,32 +36,32 @@ retry()
|
||||
local return_code=1
|
||||
|
||||
|
||||
while [[ $return_code -ne 0 && $attempts -le $max_tries ]]; do
|
||||
while [ "$return_code" -ne 0 ] && [ "$attempts" -le "$max_tries" ]; do
|
||||
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"
|
||||
for param in "${@:2}"; do P="$P '$param'"; done
|
||||
#for param in "${@:2}"; do P="$P '$param'"; done
|
||||
#TODO: replace single quotes in each arg with '"'"' ?
|
||||
export RETRY_ATTEMPT=$attempts
|
||||
bash -c "$P"
|
||||
sh -c "$P"
|
||||
return_code=$?
|
||||
#__log_out "Process returned $return_code on attempt $attempts"
|
||||
if [ $return_code -eq 127 ]; then
|
||||
# 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,12 +70,10 @@ retry()
|
||||
exit $return_code
|
||||
}
|
||||
|
||||
# If we're being sourced, don't worry about such things
|
||||
if [ "$BASH_SOURCE" == "$0" ]; then
|
||||
# Prints the help text
|
||||
|
||||
help()
|
||||
{
|
||||
local retry=$(basename $0)
|
||||
local retry=$(basename "$0")
|
||||
cat <<EOF
|
||||
Usage: $retry [options] -- execute command
|
||||
-h, -?, --help
|
||||
@ -90,14 +87,14 @@ EOF
|
||||
}
|
||||
|
||||
# show help for no arguments if stdin is a terminal
|
||||
if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" == '-h' ] || [ "$1" == '-?' ] || [ "$1" == '--help' ]
|
||||
if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" = '-h' ] || [ "$1" = '-?' ] || [ "$1" = '--help' ]
|
||||
then
|
||||
help
|
||||
exit 0
|
||||
fi
|
||||
|
||||
$GETOPT_BIN --test > /dev/null
|
||||
if [[ $? -ne 4 ]]; then
|
||||
if [ $? -ne 4 ]; then
|
||||
echo "I’m sorry, 'getopt --test' failed in this environment. Please load GNU getopt."
|
||||
exit 1
|
||||
fi
|
||||
@ -106,7 +103,7 @@ EOF
|
||||
LONGOPTIONS=verbose,tries:,sleep:,min:,max:,fail:
|
||||
|
||||
PARSED=$($GETOPT_BIN --options="$OPTIONS" --longoptions="$LONGOPTIONS" --name "$0" -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
if [ $? -ne 0 ]; then
|
||||
# e.g. $? == 1
|
||||
# then getopt has complained about wrong arguments to stdout
|
||||
exit 2
|
||||
@ -159,5 +156,3 @@ EOF
|
||||
done
|
||||
|
||||
retry "$max_tries" "$min_sleep" "$max_sleep" "$constant_sleep" "$fail_script" "$@"
|
||||
|
||||
fi
|
||||
|
Loading…
Reference in New Issue
Block a user