-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathif
More file actions
51 lines (43 loc) · 953 Bytes
/
if
File metadata and controls
51 lines (43 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
#
# tryuntilsuccess.sh N cmd arg1 arg2 ...
# ======================================
# Executes a bash command 'cmd'. If it fails it is called again. N sets a limit on the number of attempts.
#
# https://github.com/gustafsson/tryuntilsuccess.sh
set -e
N=$1
cmd=$2
shift 2
while [ $# -gt 0 ]
do
args="$args $1"
shift
done
if ! [[ "$N" =~ ^[0-9]+$ ]] ; then
echo Syntax $0 N cmd args
echo N was not a number. Got '$N'
false
fi
if [[ "$N" == "0" ]] ; then
echo Syntax $0 N cmd args
echo N bust be at least 1. Got '$N'
fi
if [ -z '$cmd' ] ; then
echo Syntax $0 N cmd args
echo cmd was empty
false
fi
# Catch any errors during the first N-1 number of attempts
for attempt in `seq 1 $(($N - 1))` ; do
echo Attempt $attempt of $N: $cmd $args
fail=
$cmd $args || fail=1
if [ -z $fail ] ; then
echo Success on attempt number $attempt
exit
fi
done
# Run without catching error
echo Attempt $N of $N: $cmd $args
$cmd $args