]> git.ipfire.org Git - thirdparty/bash.git/blob - examples/functions/repeat2
Bash-4.2 patch 45
[thirdparty/bash.git] / examples / functions / repeat2
1 # To: chet@ins.CWRU.Edu
2 # Subject: Bash functions
3 # From: Sandeep Mehta <sxm@philabs.Philips.Com>
4
5 ##########################################
6 #
7 # repeat - clone of C shell builtin `repeat'
8 #
9 # usage: repeat <count> <command>
10 #
11 # It has been tested inside other functions and in conditionals like
12 # if [ "`repeat <count> <command>`" ]; then COMMANDS [ else COMMANDS ] fi
13 # Please send me fixes/enhancements.
14 #
15 # Sandeep Mehta <sxm@philabs.Philips.Com>
16 ##########################################
17 repeat()
18 {
19 local rcount=$1
20
21 if [ $# -le 1 ] || [ -z "$rcount" ]; then
22 echo "usage: repeat <count> <command>" 1>&2
23 return 2
24 fi
25
26 shift
27
28 local acmd=("$@")
29
30 if [ $rcount -le 0 ]; then
31 echo "count must be greater than 0"
32 echo "usage: repeat <count> <command>" 1>&2
33 return 2
34 fi
35
36 st=0
37 while [ $rcount -gt 0 ]; do
38 eval "${acmd[@]}"
39 st=$?
40 rcount=$((rcount - 1))
41 done
42 return $st
43 }