bash 如何在bash中使用没有参数的getopts选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34480877/
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
How to use getopts option without argument at the end in bash
提问by Nissim
I want to use getopts in bash. The user should select his own options. most of the options gets arguments but there are 2 options that does not gets an argument. It works good when the option without the argument is at the middle of the command, but when it is at the end it does not work.
我想在 bash 中使用 getopts。用户应该选择他自己的选项。大多数选项都有参数,但有 2 个选项没有参数。当不带参数的选项在命令的中间时它工作得很好,但当它在最后时它不起作用。
Note - the first two arguments (Server and Password) are mandatory and not a part of the getopts.
注意 - 前两个参数(服务器和密码)是强制性的,不是 getopts 的一部分。
#!/bin/bash
SERVER=
PASSWORD=
VERSION=v0
ASKED_REVISION=0
DB=schema_1
PROPERTIES=1
FULL=0
USER=
# Usage info
show_help()
{
echo "
Usage: Environment DBPassword [-V Version] [-R Revision] [-S Schema] [-P] [-F] [-U Username]
-V Version Version to deploy.
-R Revision Revision number (0 means the latest revision).
-S Schema Schema in the DB to deploy.
-P No(!) External Properties. If this flag is marked the deploy will not copy the external properties
-F Full deploy
-U Username. This User who run the deploy.
For Example: ./deploy server1 123456 -V v1.0 -R 12345 -S schema1 -P -F -U User1
-H Help
"
}
OPTIND=3
while getopts ":V:R:S:P:F:U:H:" FLAG; do
case "$FLAG" in
V) # Set option "V" [Version]
VERSION=$OPTARG
;;
R) # Set option "R" [Revision]
ASKED_REVISION=$OPTARG
;;
S) # Set option "S" [Schema]
DB=$OPTARG
;;
P) # Set option "P" [No External Properties]
PROPERTIES=0
OPTIND=$OPTIND-1
;;
F) # Set option "F" [FULL]
FULL=1
OPTIND=$OPTIND-1
;;
U) # Set option "U" [User]
USER=$OPTARG
;;
H) # help
show_help
;;
\?)
echo "Invalid option: -$OPTARG. Use -H flag for help."
exit
;;
esac
done
shift $((OPTIND-1)) #This tells getopts to move on to the next argument.
echo "Env=$SERVER"
echo "Pass=$PASSWORD"
echo "Version=$VERSION"
echo "Revision=$ASKED_REVISION"
echo "Schema=$DB"
echo "External Properties=$PROPERTIES"
echo "FULL=$FULL"
echo "USER=$USER"
-F and -P shouldn't get an argument. When I run this command it works correctly (-F in the middle):
-F 和 -P 不应该争论。当我运行此命令时,它可以正常工作(中间是 -F):
./deploy_test.sh server1 pass1234 -U 555 -R 55555 -F -V v1.0
When the -F or -P are in the end of the command, This line is not working correctly (everything works good but the -F):
当 -F 或 -P 在命令的末尾时,此行无法正常工作(除 -F 外一切正常):
./deploy_test.sh server1 pass1234 -U 555 -R 55555 -V v1.0 -F
回答by Julian
Remove the colons from the options that don't require arguments, and don't manually do any shifting or changing of OPTIND
yourself within the while
/ case
statment (you'll still need to set OPTIND
prior to the while loop to skip the first 2 arguments):
从不需要参数的选项中删除冒号,并且不要OPTIND
在while
/case
语句中手动进行任何移动或更改(您仍然需要OPTIND
在 while 循环之前设置以跳过前 2 个参数):
while getopts ":V:R:S:PFU:H" FLAG; do
I've shown the while
line with the appropriate changes for the P
and F
options as well as for H
since it also wouldn't require an argument.
我已经显示了while
对P
和F
选项以及 for进行了适当更改的行,H
因为它也不需要参数。
As per the documentation:
根据文档:
if a character is followed by a colon, the option is expected to have an argument
如果一个字符后跟一个冒号,则该选项应该有一个参数
So the :
should only be there for options with arguments.
所以 the:
应该只存在于带有参数的选项中。