我正在写一个接受参数的bash脚本。 我正在使用getopts来实现它。
#!/bin/bash while getopts ":a" opt; do case $opt in a) echo "-a was triggered!" >&2 ;; \?) echo "Invalid option: -$OPTARG" >&2 ;; esac done
但上面的代码返回的是我这个错误。
'etOpts_test.sh: line 4: syntax error near unexpected token `in 'etOpts_test.sh: line 4: ` case $opt in
我正在使用CentOs 5.5
在第4行,你可能想要( $opt
引用$opt
)的case "$opt" in
。 否则,如果它包含元字符可能会失败。
它应该是a :,而不是:a表示需要参数的标志,也不应该引用问号,因为它用作通配符。 总体代码将(也显示了一个标志-h不采取论据):
function usage { echo "usage: ..." } a_arg= while getopts a:h opt; do case $opt in a) a_arg=$OPTARG ;; h) usage && exit 0 ;; ?) usage && exit 2 ;; esac done