diff options
author | EuAndreh <eu@euandre.org> | 2022-08-13 09:41:23 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-08-13 09:41:23 -0300 |
commit | dbfaa8e6174c2076e61244c344c82303d80db1ae (patch) | |
tree | d251d1065494d3416ef1cc3ed7ddc14a3dd3a1be /bin/n-times | |
parent | bin/{uc,lc}: Add working utilities (diff) | |
download | dotfiles-dbfaa8e6174c2076e61244c344c82303d80db1ae.tar.gz dotfiles-dbfaa8e6174c2076e61244c344c82303d80db1ae.tar.xz |
bin/n-times: Add working utility
Diffstat (limited to 'bin/n-times')
-rwxr-xr-x | bin/n-times | 73 |
1 files changed, 73 insertions, 0 deletions
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 |