4 # repeat: repeat a command.
5 # @(#) repeat.ksh 1.1 93/06/03
6 # 90/05 john h. dubois iii (john@armory.com)
8 # 93/06/03 Added s, h, p, and v options
10 # conversion to bash v2 syntax done by Chet Ramey
24 echo "$name: repeatedly execute a command line.
26 commandline is executed once for each integer from startcount through endcount
27 inclusive. The default for startcount is 1 if a positive endcount or no
28 endcount is given, and -1 if a negative endcount is given. A count
29 parameter consisting of a single number is taken to be an endcount. If
30 only an endcount is given and it is positive, commandline is executed
31 endcount times. endcount may be less than startcount. If no endcount is
32 given (e.g. a count parameter of \"10-\"), commandline execution repeats
33 indefinitely with the iteration variable incrementing in a positive
34 direction. A count parameter of consisting of \"-\" will repeat
35 indefinitely starting with 1.
37 Note that quoting and variables in commandline are interpreted twice, once
38 when it is passed to the repeat command, and once when it is actually executed.
40 The iteration variable is \"count\". If \$count is used in commandline, make
41 sure it is quoted with ' or \.
45 -p: Print value of iteration variable on stderr before each iteration.
46 -s <sec>: sleep for <sec> seconds after each iteration except the last.
47 -v: Print start and end values before beginning."
51 Usage
="Usage: repeat [-hpv] [-s <sec>] [[startcount]-][endcount] command [arg ...]"
53 typeset
-i count
=1 forever
=0 sleep=0 print
=0 verbose
=0
55 while getopts :0123456789hpvs
: opt
; do
58 s
) sleep=$OPTARG ||
exit 1;;
62 +?
) echo "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
63 ?
) echo "$name: $OPTARG: bad option. Use -h for help." 1>&2; exit 2;;
67 # remove args that were options
71 echo -e "$Usage\nUse -h for help." 1>&2
82 -[0-9]*-[0-9]*|
[0-9]*-[0-9]*)
99 echo "$name: bad count parameter: $1" 1>&2
106 [ -z "$end" ] && [ $count -le "$end" ] && increment
=1 || increment
=-1
108 istrue
$verbose && echo "start=$count end=$end" 1>&2
110 # Need to do this here so that up to this point, -0 will keep the leading -
111 # and end will not be 0 if no value assigned
114 let end
+=increment
# make loop inclusive of original endcount
116 while istrue
$forever ||
[ $count -ne $end ]; do
117 istrue
$print && echo $count 1>&2
119 istrue
$sleep && sleep $sleep