[英]send an hyphened/double-hyphened argument to getopts
I'm using getopts to get optional arguments in a shell script, and then I use them to run another script.我正在使用 getopts 在 shell 脚本中获取可选的 arguments,然后我使用它们运行另一个脚本。 one of the arguments I'd like to send to the other program is an optional argument (eg --conf).
我想发送给另一个程序的 arguments 之一是可选参数(例如 --conf)。
but when I'm sending it to getopts i get these error messages:但是当我将它发送到 getopts 时,我收到以下错误消息:
service_wrapper.sh: illegal option -- -
service_wrapper.sh: illegal option -- c
service_wrapper.sh: illegal option -- o
service_wrapper.sh: illegal option -- n
service_wrapper.sh: illegal option -- f
is there any way I can send this argument to getopts?有什么办法可以将此参数发送给getopts?
this is the code parsing the options:这是解析选项的代码:
while getopts 'p:o' OPTION
do
case ${OPTION} in
o) CONF=$OPTARG;;
p) PLATFORM=$OPTARG;;
esac
done
this is an example of the line I'm trying to run:这是我试图运行的行的一个例子:
./service_wrapper.sh config -p platform -o '--full-config-data {"json_key1":"json_val1","json_key2":"json_val2"}'
EDIT :编辑:
as suggested in the comments I've printed the bash version and the args this is the output:正如评论中所建议的,我已经打印了 bash 版本和参数,这是 output:
############################
BASH:4.2.46(2)-release
arg: config
arg: -p
arg: platform
arg: -o
arg: --full-config-data {"json_key1":"json_val1","json_key2":"json_val2"}
############################
Got it, After printing the args as @user1934428 suggested.知道了,按照@user1934428 的建议打印参数后。 I've figured out that the last argument is available to the script but not to getopts.
我发现最后一个参数可用于脚本但不可用于 getopts。
I've tried to understand why and it hit me, a colon is missing after the 'o'!我试图理解为什么并且它击中了我,'o'之后缺少一个冒号!
Instead of while getopts 'p:o' OPTION
it should be while getopts 'p:o:' OPTION
而不是
while getopts 'p:o' OPTION
它应该是while getopts 'p:o:' OPTION
as noted in getopts man page :如getopts 手册页所述:
if a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space.
如果一个字母后跟一个冒号,则该选项应该有一个参数,或一组 arguments,必须用空格与其分隔。
Note: with the information above I fixed the issue and getopts got the last argument, though it parsed it by spaces, even though it was quoted.注意:根据上面的信息,我解决了这个问题,getopts 得到了最后一个参数,尽管它用空格解析它,即使它被引用了。 It because i sent arguments to main function like this
main $@
when i shouldv'e quoted it like this main "$@"
.这是因为我将 arguments 发送到主 function 就像这个
main $@
当我应该像这个main "$@"
引用它时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.