better getopts
authorTomas Mudrunka <tomas@mudrunka.cz>
Fri, 5 Aug 2016 20:01:10 +0000 (22:01 +0200)
committerTomas Mudrunka <tomas@mudrunka.cz>
Fri, 5 Aug 2016 20:01:10 +0000 (22:01 +0200)
bash/getopts.sh

index 270410ae89dac1d7f1c07c8c70d0c6e709572b60..f65d50ba73ec76dd298d1cafef8f84e31f364092 100755 (executable)
@@ -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
This page took 0.216388 seconds and 4 git commands to generate.