bash - getopts no argument provided -


how check whether there no required argument provided? found ":" option in switch case should sufficient purpose, never enters case (codeblock). doesn't matter whether put "colon-case" @ beginning or elsewhere.

my code:

while getopts :a:b: option;      case "$option" in          a)              var1=$optarg              ;;          b)              var2=$optarg              ;;          ?)              exitscript "`echo "invalid option $optarg"`" "5"              ;;          :)              exitscript "`echo "option -$optarg requires argument."`" "5"              ;;          *)              exitscript "`echo "option $optarg unrecognized."`" "5"              ;;      esac done 

thx in advance.

you must escape ?. next can (partially) works.

err() { 1>&2 echo "$0: error $@"; return 1; } while getopts ":a:b:" opt;         case $opt in                 a) aarg="$optarg" ;;                 b) barg="$optarg" ;;                 :) err "option -$optarg requires argument." || exit 1 ;;                 \?) err "invalid option: -$optarg" || exit 1 ;;         esac done  shift $((optind-1)) echo "arg :$aarg:" echo "arg b :$barg:" echo "unused parameters:$@:" 

partially because when call above script as

$ bash script -a a_arg -b b_arg 

will works expect,

arg :a_arg: arg b :b_arg: unused parameters:extra: 

but when call as

bash script -a -b b_arg 

will prints

arg :-b: arg b :: unused parameters:b_arg: 

what not, want.

and uuoe. (useles use of echo).


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -