【发布时间】:2012-09-29 04:22:11
【问题描述】:
可能重复:
Using getopts in bash shell script to get long and short command line options
我正在尝试弄清楚如何使用诸如 -e/--email -h/--help 之类的标志。
更新: 当前示例:
while getopts ":ec:h" OptionArgument; do
case $OptionArgument in
e ) S1=${OPTARG};;
c ) S2=${OPTARG};;
h ) usage;;
\?) usage;;
* ) usage;;
esac
done
但是,如果我留空,它什么也不做。如果我添加 -h 它仍然会运行并且不会显示使用情况。
更新2
while [ $1 ]; do
case $1 in
'-h' | '--help' | '?' )
usage
exit
;;
'--conf' | '-c' )
;;
'--email' | '-e' )
EMAIL=$1
;;
* )
usage
exit
;;
esac
shift
done
-h/--help 有效,但其他一切都失败并显示使用情况。
【问题讨论】:
-
Bash 的
getopts不支持长参数,只支持一个字母。请参阅this answer 了解 GNU 的getopt来做你想做的事。 -
更新了问题,到目前为止它成功了一半
-
对于
--email的情况,您需要一个额外的shift和EMAIL=$2
标签: bash shell getopt getopt-long getopts