From 90eaebabcaaea74237f34cf05709625345f276cc Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 12 May 2022 12:01:54 -0300 Subject: Move Git repository into ~/.usr/.git/ --- bin/clamp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 bin/clamp (limited to 'bin/clamp') diff --git a/bin/clamp b/bin/clamp new file mode 100755 index 0000000..a40b823 --- /dev/null +++ b/bin/clamp @@ -0,0 +1,74 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + clamp NUMBER MIN MAX + clamp -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -h, --help show this message + + Clamp the NUMBER between MIN and MAX. + 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)) + +assert_arg() { + if [ -z "$1" ]; then + printf 'Missing %s\n\n' "$2" >&2 + usage >&2 + exit 2 + fi +} + +NUMBER="${1:-}" +MIN="${2:-}" +MAX="${3:-}" + +assert_arg "$NUMBER" 'NUMBER' +assert_arg "$MIN" 'MIN' +assert_arg "$MAX" 'MAX' + + +if [ "$MIN" -gt "$MAX" ]; then + printf 'MIN (%s) is greater then MAX (%s).\n' "$MIN" "$MAX" >&2 + exit 2 +fi + +min -- "$(max -- "$NUMBER" "$MIN")" "$MAX" -- cgit v1.2.3