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 | 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 | ;;
|
---|
102 | esac
|
---|
103 |
|
---|
104 | shift
|
---|
105 |
|
---|
106 | [ -z "$end" ] && [ $count -le "$end" ] && increment=1 || increment=-1
|
---|
107 |
|
---|
108 | istrue $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
|
---|
112 | typeset -i end
|
---|
113 |
|
---|
114 | let end+=increment # make loop inclusive of original endcount
|
---|
115 |
|
---|
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
|
---|