aboutsummaryrefslogtreecommitdiff
path: root/bin/max
diff options
context:
space:
mode:
Diffstat (limited to 'bin/max')
-rwxr-xr-xbin/max84
1 files changed, 84 insertions, 0 deletions
diff --git a/bin/max b/bin/max
new file mode 100755
index 0000000..ae38983
--- /dev/null
+++ b/bin/max
@@ -0,0 +1,84 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ max NUMBER...
+ max -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+ Options:
+ -h, --help show this message
+
+ Get the maximum number from the given values.
+
+
+ Examples:
+
+ Get the maximum number from a list:
+
+ $ min 5 3 9 9 4
+ 9
+
+ Get the maximum number when negative numbers are given
+
+ $ max -- -3 -5
+ -3
+
+ Get the maximum number given a single number
+
+ $ max 8
+ 8
+
+ The maximum default number:
+
+ $ max
+ 0
+ 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))
+
+if [ -z "${1:-}" ]; then
+ echo 0
+ exit
+fi
+
+N="$1"
+for n in "$@"; do
+ N=$((N > n ? N : n))
+done
+echo "$N"