add proper error handling for all final exec calls
[hband-tools.git] / user-tools / repeat
blob6c7d5e119da1ef17411b3045e72275e20b037435
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 repeat - Run a give command repeatedly
10 =head1 SYNOPSIS
12 repeat I<COMMAND> [I<ARGS>]
14 =head1 ENVIRONMENT
16 =over 4
18 =item REPEAT_TIMES
20 How many times to repeat the given command.
21 Default is -1 which means infinite.
23 =item REPEAT_COUNT
25 How many times the command has been ran.
26 It is not a variable repeat(1) itself takes as input,
27 but passes to I<COMMAND> for its information.
29 =item REPEAT_UNTIL
31 Stop repeat(1) if I<COMMAND> exists with this return code.
32 By default the return code is not checked.
34 =item REPEAT_DELAY
36 Sleep interval between invocations.
37 In seconds, by default.
38 See sleep(1) for valid parameters, eg. "10m" for 10 minutes.
39 Default is no delay.
41 =back
43 =cut
45 EOF
48 . /usr/lib/tool/bash-utils
50 if [ "$1" = --help -o $# = 0 ]
51 then
52 echo "Usage: [REPEAT_TIMES=num] [REPEAT_DELAY=interval] [REPEAT_UNTIL=code] repeat <COMMAND> [<ARGS>]"
53 exit 0
56 REPEAT_TIMES=${REPEAT_TIMES:--1}
57 export REPEAT_COUNT
58 REPEAT_COUNT=0
60 while [ $REPEAT_TIMES = -1 -o $REPEAT_COUNT -lt $REPEAT_TIMES ]
62 "$@"
63 if [ $? = "$REPEAT_UNTIL" ]
64 then
65 break
67 REPEAT_COUNT=$[REPEAT_COUNT + 1]
68 sleep ${REPEAT_DELAY:-0}
69 done