getopts command not working at expected Hi: I am writing a script using getopts, running in "sh" shell HP-UX 11.11. My optstring is like this :gru:f: However, during script tests, when invoked like this: script -g -u -f it places the string "-f" in OPTARG variabl...
OPT_C=$OPTARG echo "-c used: $OPTARG" echo "OPT_C = $OPT_C" ;; d) #set option "d" OPT_D=$OPTARG echo "-d used: $OPTARG" echo "OPT_D = $OPT_D" ;; h) #show help HELP ;; \?) #unrecognized option - show help echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allo...
12345 $ ./getopts.sh -a -b aardvark -c -v -v -va: onb: aardvarkc: onverbosity: 3 All flags grouped together for the same effect (order does not matter) 12345 $ ./getopts.sh -vvvacb aardvarka: onb: aardvarkc: onverbosity: 3 Some flags grouped, some separated; default values us...
This is where getopts comes in. getopts, by the way, is not to be confused with the getopt command. No, getopts is built into Bash "to parse command line arguments to a script." However, the practical purpose is to save you from writing all kinds of code to allow for every conceivabl...
) echo "${SCRIPT_NAME}: -$OPTARG: unknown option" >&2 && usage >&2 && exit 99 ;; esac done shift $((${OPTIND} - 1)) 这是一个测试: # Short options test $ ./foobar_any_getopts.sh -bF "Hello world" -B 6 file1 file2 foo=0 bar=1 barfoo=6 foobar=1 foobar_name=Hello...
测试OPTARG。 这很聪明,但它带有警告: getopts不能强制执行opt规范。如果用户提供无效选项,则无法返回错误。在解析OPTARG时,您必须自己进行错误检查。 OPTARG用于长选项名称,当您的长选项本身具有参数时,这会使用量变得复杂。您最终必须自己编写代码作为附加案例。 因此,尽管可以编写更多代码来解决缺乏对长选项的支持的...