Patch-ID: bash40-021
[bash.git] / examples / functions / repeat2
blob2e2dc7a75a4dd9431d54cb07d72081138e68b41d
1 # To: chet@ins.CWRU.Edu
2 # Subject: Bash functions
3 # From: Sandeep Mehta <sxm@philabs.Philips.Com>
5 ##########################################
7 # repeat - clone of C shell builtin `repeat'
9 # usage: repeat <count> <command>
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()
19         local rcount=$1
21         if [ $# -le 1 ] || [ -z "$rcount" ]; then
22                 echo "usage: repeat <count> <command>" 1>&2
23                 return 2
24         fi
26         shift
28         local acmd=("$@")
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
36         st=0
37         while [ $rcount -gt 0 ]; do
38                 eval "${acmd[@]}"
39                 st=$?
40                 rcount=$((rcount - 1))
41         done
42         return $st