I have a new RHEL6 machine and I'm trying to run a script to generate some output. The script uses getopts which I've never used in the past. This should have worked on other machines but is my first time trying it. Below is the beginning of the script. Is there anything wrong with the syntax? When I try to output the variables it displays nothing:
我有一台新的RHEL6机器,我正在尝试运行一个脚本来生成一些输出。该脚本使用了我以前从未使用过的getopts。这应该适用于其他机器,但这是我第一次尝试它。下面是脚本的开头。语法有什么问题吗?当我尝试输出变量时,它什么都不显示:
#! /bin/sh
while getopts "h:u:g:o:e:y:bf" c
do
case "$c" in
u) USER=$OPTARG;;
g) GROUP=$OPTARG;;
o) OUT=$OPTARG;;
b) BATCH=1;;
f) FORCE=1;;
h) FQDN=$OPTARG;;
e) ENTITYID=$OPTARG;;
y) YEARS=$OPTARG;;
\?) echo "keygen [-o output directory (default .)] [-u username to own keypair] [-g owning groupname] [-h hostname for cert] [-y years to issue cert] [-e entityID to embed in cert]"
exit 1;;
esac
done
echo $FQDN
The echo displays a blank line.
回显显示一个空白行。
1 个解决方案
#1
1
You can't use question mark with the bash getopts
(you also can't use the colon). In the case of question mark, getopts
sets the value of the argument ($c
in your case) to a question mark when the end of options has been encountered. It also uses question mark and colon as the value for the argument name when there's an error (specifically, ? is used when an invalid option is encountered or when in non-silent mode and a required option is not provided; colon is used in silent mode when a required option is not provided). In those error cases, OPTARG contains the offending argument. This is how POSIX getopts works as well.
你不能在bash getopts上使用问号(你也不能使用冒号)。在问号的情况下,当遇到选项结束时,getopts将参数的值(在您的情况下为$ c)设置为问号。当出现错误时,它还使用问号和冒号作为参数名称的值(具体来说,遇到无效选项时使用?或非静默模式时使用?并且未提供必需选项;冒号用于静默未提供所需选项时的模式)。在这些错误情况下,OPTARG包含有问题的参数。这也是POSIX getopts的工作原理。
The KSH getopts behaves differently, but it also excludes ? :
(as well as - [ ]
and only allowing #
as the first option). It does, however, show a usage message when you provide -?
. Basically, don't use -? with shell getopts. :)
KSH的上瘾行为有所不同,但它也排除了? :(以及 - []并且只允许#作为第一个选项)。但是,当您提供 - 时,它会显示一条用法消息。基本上,不要使用 - ?用shell getopts。 :)
Typically, I write a small function called "usage" and call it from both *)
and by checking $?
immediately after the case statement for non-zero value.
通常,我会编写一个名为“usage”的小函数,并从*)调用它并检查$?在案例陈述之后立即为非零值。
#1
1
You can't use question mark with the bash getopts
(you also can't use the colon). In the case of question mark, getopts
sets the value of the argument ($c
in your case) to a question mark when the end of options has been encountered. It also uses question mark and colon as the value for the argument name when there's an error (specifically, ? is used when an invalid option is encountered or when in non-silent mode and a required option is not provided; colon is used in silent mode when a required option is not provided). In those error cases, OPTARG contains the offending argument. This is how POSIX getopts works as well.
你不能在bash getopts上使用问号(你也不能使用冒号)。在问号的情况下,当遇到选项结束时,getopts将参数的值(在您的情况下为$ c)设置为问号。当出现错误时,它还使用问号和冒号作为参数名称的值(具体来说,遇到无效选项时使用?或非静默模式时使用?并且未提供必需选项;冒号用于静默未提供所需选项时的模式)。在这些错误情况下,OPTARG包含有问题的参数。这也是POSIX getopts的工作原理。
The KSH getopts behaves differently, but it also excludes ? :
(as well as - [ ]
and only allowing #
as the first option). It does, however, show a usage message when you provide -?
. Basically, don't use -? with shell getopts. :)
KSH的上瘾行为有所不同,但它也排除了? :(以及 - []并且只允许#作为第一个选项)。但是,当您提供 - 时,它会显示一条用法消息。基本上,不要使用 - ?用shell getopts。 :)
Typically, I write a small function called "usage" and call it from both *)
and by checking $?
immediately after the case statement for non-zero value.
通常,我会编写一个名为“usage”的小函数,并从*)调用它并检查$?在案例陈述之后立即为非零值。