From: Tomas Mudrunka Date: Fri, 5 Aug 2016 20:01:10 +0000 (+0200) Subject: better getopts X-Git-Url: http://git.harvie.cz/?p=mirrors%2FPrograms.git;a=commitdiff_plain;h=b16422a7f8b70676cef7a203f83f5d03ce66e89e better getopts --- diff --git a/bash/getopts.sh b/bash/getopts.sh index 270410a..f65d50b 100755 --- a/bash/getopts.sh +++ b/bash/getopts.sh @@ -1,7 +1,18 @@ #!/bin/sh -while getopts "a:b:c:d:" OPT; do +while getopts ":a:b:c:d:f" OPT; do echo "$OPT ==> $OPTARG"; + case $OPT in + a ) ahoj=$OPTARG;; + + '?') echo "Unknown option: -$OPTARG" >&2; exit 1;; + ':') echo "Missing option argument for -$OPTARG" >&2; exit 2;; + * ) echo "Unimplemented option: -$OPT" >&2; exit 3;; + esac done +echo shift $(($OPTIND -1)); echo "Rest: $@"; + +: ${ahoj:=default} +echo ahoj: $ahoj