source: vendor/bash/3.1-p17/examples/scripts.v2/repeat

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 3.0 KB
Line 
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
12istrue()
13{
14 test 0 -ne "$1"
15}
16
17isfalse()
18{
19 test 0 -eq "$1"
20}
21
22phelp()
23{
24echo "$name: repeatedly execute a command line.
25$Usage
26commandline is executed once for each integer from startcount through endcount
27inclusive. The default for startcount is 1 if a positive endcount or no
28endcount is given, and -1 if a negative endcount is given. A count
29parameter consisting of a single number is taken to be an endcount. If
30only an endcount is given and it is positive, commandline is executed
31endcount times. endcount may be less than startcount. If no endcount is
32given (e.g. a count parameter of \"10-\"), commandline execution repeats
33indefinitely with the iteration variable incrementing in a positive
34direction. A count parameter of consisting of \"-\" will repeat
35indefinitely starting with 1.
36
37Note that quoting and variables in commandline are interpreted twice, once
38when it is passed to the repeat command, and once when it is actually executed.
39
40The iteration variable is \"count\". If \$count is used in commandline, make
41sure it is quoted with ' or \.
42
43Options:
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
50name=${0##*/}
51Usage="Usage: repeat [-hpv] [-s <sec>] [[startcount]-][endcount] command [arg ...]"
52
53typeset -i count=1 forever=0 sleep=0 print=0 verbose=0
54
55while 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
65done
66
67# remove args that were options
68shift $((OPTIND-1))
69
70if [ $# -lt 2 ]; then
71 echo -e "$Usage\nUse -h for help." 1>&2
72 exit 2
73fi
74
75case "$1" in
76-[0-9]*-|[0-9]*-)
77 # Start value only
78 count=${1%-}
79 forever=1
80 end="-1";
81 ;;
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}
87 ;;
88-[0-9]*|[0-9]*)
89 end=$1
90 case "$end" in
91 -\*) count=-1;;
92 esac
93 ;;
94-)
95 forever=1
96 end="-1";
97 ;;
98*)
99 echo "$name: bad count parameter: $1" 1>&2
100 exit 1
101 ;;
102esac
103
104shift
105
106[ -z "$end" ] && [ $count -le "$end" ] && increment=1 || increment=-1
107
108istrue $verbose && echo "start=$count end=$end" 1>&2
109
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
112typeset -i end
113
114let end+=increment # make loop inclusive of original endcount
115
116while istrue $forever || [ $count -ne $end ]; do
117 istrue $print && echo $count 1>&2
118 eval "$@"
119 istrue $sleep && sleep $sleep
120 let count+=increment
121done
Note: See TracBrowser for help on using the repository browser.