aboutsummaryrefslogtreecommitdiff
path: root/bin/rep.sh
blob: e53cbac3786dca092a4a590b14a7eb78bec40db0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Repeat a command
self=rep

# Check arguments.
if [ "$#" -lt 2 ] ; then
    printf >&2 '%s: Need a count and a program name\n' "$self"
    exit 2
fi

# Shift off the repetition count.
c=$1
shift

# Check the repetition count looks sane. Zero is fine!
if [ "$c" -lt 0 ] ; then
    printf >&2 '%s: Nonsensical negative count\n' "$self"
    exit 2
fi

# Run the command the specified number of times. Stop immediately as soon as a
# run fails.
while [ "${n=1}" -le "$c" ] ; do
    "$@" || exit
    n=$((n+1))
done