#!/bin/sh -e
#
# rep - 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
