aboutsummaryrefslogtreecommitdiff
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  max NUMBER...
		  max -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -h, --help    show this message

		  NUMBER        the list of numbers to get the max from


		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
printf '%s\n' "$N"