Imported from ../bash-2.0.tar.gz.
[platform/upstream/bash.git] / examples / scripts.v2 / repeat
1 #! /bin/bash
2 #
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
9 #
10 # conversion to bash v2 syntax done by Chet Ramey
11
12 istrue()
13 {
14         test 0 -ne "$1"
15 }
16
17 isfalse()
18 {
19         test 0 -eq "$1"
20 }
21
22 phelp()
23 {
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.
36
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.
39
40 The iteration variable is \"count\".  If \$count is used in commandline, make
41 sure it is quoted with ' or \.
42
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."
48 }
49
50 name=${0##*/}
51 Usage="Usage: repeat [-hpv] [-s <sec>] [[startcount]-][endcount] command [arg ...]"
52
53 typeset -i count=1 forever=0 sleep=0 print=0 verbose=0
54
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
66  
67 # remove args that were options
68 shift $((OPTIND-1))
69
70 if [ $# -lt 2 ]; then
71     echo -e "$Usage\nUse -h for help." 1>&2
72     exit 2
73 fi
74
75 case "$1" in
76 -[0-9]*-|[0-9]*-)
77     # Start value only
78     count=${1%-}
79     forever=1
80     ;;
81 -[0-9]*-[0-9]*|[0-9]*-[0-9]*)
82     # Start and end value
83     s=${1%-}
84     end=${s##[0-9]*-}
85     count=${s%-$end}
86     ;;
87 -[0-9]*|[0-9]*)
88     end=$1
89     case "$end" in
90     -\*)        count=-1;;
91     esac
92     ;;
93 -)
94     forever=1
95     ;;
96 *) 
97     echo "$name: bad count parameter: $1" 1>&2
98     exit 1
99     ;;
100 esac
101
102 shift
103
104 [ -z "$end" -o $count -le "$end" ] && increment=1 || increment=-1
105
106 istrue $verbose && echo "start=$count end=$end" 1>&2
107
108 # Need to do this here so that up to this point, -0 will keep the leading -
109 # and end will not be 0 if no value assigned
110 typeset -i end
111
112 let end+=increment      # make loop inclusive of original endcount
113
114 while istrue $forever || [ $count -ne $end ]; do
115     istrue $print && echo $count 1>&2
116     eval "$@"
117     istrue $sleep && sleep $sleep
118     let count+=increment
119 done