aboutsummaryrefslogtreecommitdiff
#!/bin/sh
set -eu

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

help() {
	cat <<-'EOF'


		Options:
		  -h, --help    show this message

		  NUMBER        the list of numbers to get the min from


		Get the minimun number from the given values.


		Examples:

		  Get the minimum number from a list:

		    $ min 5 3 9 9 4
		    3


		  Get the minimum number when negative numbers are given

		    $ min -- -3 -5
		    -5


		  Get the minimum number given a single number

		    $ min 8
		    8


		  The minimum default number:

		    $ min
		    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"