Patch-ID: bash41-003
[bash.git] / examples / scripts.v2 / repeat
blobb6fccac6747e3dc7be2a240202091506de889f01
1 #! /bin/bash
3 # original from:
4 # repeat: repeat a command.
5 # @(#) repeat.ksh 1.1 93/06/03
6 # 90/05 john h. dubois iii (john@armory.com)
7 # 90/11 added help
8 # 93/06/03 Added s, h, p, and v options
10 # conversion to bash v2 syntax done by Chet Ramey
12 istrue()
14 test 0 -ne "$1"
17 isfalse()
19 test 0 -eq "$1"
22 phelp()
24 echo "$name: repeatedly execute a command line.
25 $Usage
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 \.
43 Options:
44 -h: Print this help.
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."
50 name=${0##*/}
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
56 case $opt in
57 h) phelp; exit 0;;
58 s) sleep=$OPTARG || exit 1;;
59 p) print=1;;
60 v)verbose=1;;
61 [0-9]) break;;
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;;
64 esac
65 done
67 # remove args that were options
68 shift $((OPTIND-1))
70 if [ $# -lt 2 ]; then
71 echo -e "$Usage\nUse -h for help." 1>&2
72 exit 2
75 case "$1" in
76 -[0-9]*-|[0-9]*-)
77 # Start value only
78 count=${1%-}
79 forever=1
80 end="-1";
82 -[0-9]*-[0-9]*|[0-9]*-[0-9]*)
83 # Start and end value
84 s=${1%-}
85 end=${s##[0-9]*-}
86 count=${s%-$end}
88 -[0-9]*|[0-9]*)
89 end=$1
90 case "$end" in
91 -\*) count=-1;;
92 esac
95 forever=1
96 end="-1";
98 *)
99 echo "$name: bad count parameter: $1" 1>&2
100 exit 1
102 esac
104 shift
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
112 typeset -i end
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
118 eval "$@"
119 istrue $sleep && sleep $sleep
120 let count+=increment
121 done