bash 使用 GetOpts 和强制参数验证命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29810297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command line arguments validation with GetOpts and mandatory parameters
提问by donhector
I'm creating a basic script that should take 3 mandatory command line options and each one must be followed by a value. Like this:
我正在创建一个基本脚本,它应该采用 3 个强制命令行选项,并且每个选项后面都必须跟一个值。像这样:
$ myscript.sh -u <username> -p <password> -f <hosts.txt>
I'm trying to make sure the user is passing those exact 3 options and their values and nothing else, otherwise I want to print the usage message and exit.
我试图确保用户传递的是那些确切的 3 个选项及其值,而不是其他任何东西,否则我想打印使用消息并退出。
I've been reading on getopts
and came up with this:
我一直在阅读getopts
并想出了这个:
usage () { echo "Usage : $ myscript.sh 1 2 3 4 5 6
-u <username> -p <password> -f <hostsFile>"; }
if [ $# -ne 6 ]
then
usage
exit 1
fi
while getopts u:p:f: opt ; do
case $opt in
u) USER_NAME=$OPTARG ;;
p) USER_PASSWORD=$OPTARG ;;
f) HOSTS_FILE=$OPTARG ;;
*) usage; exit 1;;
esac
done
echo "USERNAME: $USER_NAME"
echo "PASS: $USER_PASSWORD"
echo "FILE: $HOSTS_FILE"
I was hoping that if I do not pass any of my 3 "mandatory" options (i.e: -u -p -f) Optargs validation would catch that via the "*)
" case. While that is true for other options such "-a","-b", etc.. does not seem to be the case in this particular case:
我希望如果我没有通过我的 3 个“强制”选项(即:-u -p -f)中的任何一个,Optargs 验证将通过“ *)
”案例捕获它。虽然对于其他选项(如“-a”、“-b”等)也是如此……但在这种特殊情况下似乎并非如此:
$ myscript.sh -u <username> -p <password> -f <hosts.txt>
Getops does not treat that as invalid input and the script moves on executing the echo
commands showing 3 empty variables.
Getops 不会将其视为无效输入,脚本继续执行echo
显示 3 个空变量的命令。
How can I capture the input above as being invalid as it is not in the form of:
我如何将上面的输入捕获为无效,因为它不是以下形式:
if [ ! "$USER_NAME" ] || [ ! "$USER_PASSWORD" ] || [ ! "$HOSTS_FILE" ]
then
usage
exit 1
fi
Thanks!
谢谢!
回答by John1024
getopts
has no concept of "mandatory" options. The colons in u:p:f:
mean that, if one of those options happens to be supplied, then an argument to that option is mandatory. The option-argument pairs, however, are always optional.
getopts
没有“强制”选项的概念。中的冒号u:p:f:
表示,如果恰好提供了这些选项之一,则该选项的参数是强制性的。但是,选项参数对始终是可选的。
You can require that the user provide all three though with code such as:
您可以要求用户提供所有三个代码,例如:
##代码##Place this code after the while getopts
loop.
将此代码放在while getopts
循环之后。
The Role of *)
的作用 *)
I was hoping that if I do not pass any of my 3 "mandatory" options (i.e: -u -p -f) Optargs validation would catch that via the "*)" case.
我希望如果我没有通过我的 3 个“强制”选项(即:-u -p -f)中的任何一个,Optargs 验证将通过“*)”案例捕获它。
The *)
case is executed only if an option other than-u
, -p
, or -f
is supplied. Thus, if someone supplied, for example a -z
argument, then that case would run.
*)
仅当提供了-u
、-p
、 或以外的选项时才执行该案例-f
。因此,如果有人提供了,例如一个-z
论点,那么这个案例就会运行。