From dbfaa8e6174c2076e61244c344c82303d80db1ae Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 13 Aug 2022 09:41:23 -0300 Subject: bin/n-times: Add working utility --- bin/n-times | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 bin/n-times (limited to 'bin/n-times') diff --git a/bin/n-times b/bin/n-times new file mode 100755 index 0000000..4fa8b96 --- /dev/null +++ b/bin/n-times @@ -0,0 +1,73 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + n-times COUNT -- COMMAND... + n-times -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -h, --help show this message + + COUNT the number of times for COMMAND to be executed + + + Examples: + + Print 123 5 times: + + $ n-times 5 echo 123 + EOF +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + +COUNT="${1:-}" +shift + +eval "$(assert-arg "$COUNT" 'COUNT')" + + +while true; do + if [ "$COUNT" = 0 ]; then + break + fi + COUNT=$((COUNT - 1)) + "$@" +done -- cgit v1.2.3