为什么 getopts in bash yield : (per docs) 当一个选项缺少它的关联参数时?
Why doesn't getopts in bash yield : (per docs) when an option is missing its associated argument?
我是这个领域的新手,我 运行 我的 shell 脚本名为 "statsrandomrun.sh",包含以下代码片段:
#!/bin/bash
while getopts "m:s:xh" opt; do
case $opt in
m)
MU=$OPTARG; mflag=true; ;;
s)
SIGMA=$OPTARG; sflag=true; ;;
h)
usage; exit;;
x)
xflag=true ;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "HaHa! Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
但似乎每当我通过这个命令时
/.statsrandomrun.sh -m
没见过
echo "HaHa! Option -$OPTARG requires an argument." >&2
工作,我在上面的代码片段中实现了。我的 bash 刚刚显示了一些其他消息:
option requires an argument -- m
我显然没有把它放在我的代码中。还有另一行
Invalid option: -
我希望这会发生,因为 help getopts
的输出包含以下内容:
If a required argument is not found, getopts places a ':' into NAME and sets OPTARG to the option character found.
这是怎么回事?有任何想法吗?
如果能提前提供帮助,我将不胜感激:)
根据文档,help getopts
,
getopts: getopts optstring name [arg]
OPTSTRING contains the option letters to be recognized; if a letter
is followed by a colon, the option is expected to have an argument,
which should be separated from it by white space.
getopts reports errors in one of two ways. If the first character
of OPTSTRING is a colon, getopts uses silent error reporting. In
this mode, no error messages are printed. ................ If a
required argument is not found, getopts places a ':' into NAME and
sets OPTARG to the option character found. If getopts is not in
silent mode, ........... If a required argument is not found, a '?'
is placed in NAME, OPTARG is unset, and a diagnostic message is
printed.
当我在 非静音模式 中实现我的 getopts 函数时, /.statsrandomrun.sh -m
触发 ?
后缺少的参数被存储在 $Name
(在我的例子中 $opt
),$OPTARG
中没有内容。
所以执行了这几行代码:
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
生成 Invalid option: -
以及 内置诊断 消息。
为了得到我想要的结果,我应该使用静音模式,如
#!/bin/bash
while getopts ":m:s:xh" opt; do
#codes
done
通过在 OPTSTRING 中添加前面的冒号。
我是这个领域的新手,我 运行 我的 shell 脚本名为 "statsrandomrun.sh",包含以下代码片段:
#!/bin/bash
while getopts "m:s:xh" opt; do
case $opt in
m)
MU=$OPTARG; mflag=true; ;;
s)
SIGMA=$OPTARG; sflag=true; ;;
h)
usage; exit;;
x)
xflag=true ;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "HaHa! Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
但似乎每当我通过这个命令时
/.statsrandomrun.sh -m
没见过
echo "HaHa! Option -$OPTARG requires an argument." >&2
工作,我在上面的代码片段中实现了。我的 bash 刚刚显示了一些其他消息:
option requires an argument -- m
我显然没有把它放在我的代码中。还有另一行
Invalid option: -
我希望这会发生,因为 help getopts
的输出包含以下内容:
If a required argument is not found, getopts places a ':' into NAME and sets OPTARG to the option character found.
这是怎么回事?有任何想法吗? 如果能提前提供帮助,我将不胜感激:)
根据文档,help getopts
,
getopts: getopts optstring name [arg]
OPTSTRING contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space.
getopts reports errors in one of two ways. If the first character of OPTSTRING is a colon, getopts uses silent error reporting. In this mode, no error messages are printed. ................ If a required argument is not found, getopts places a ':' into NAME and sets OPTARG to the option character found. If getopts is not in silent mode, ........... If a required argument is not found, a '?' is placed in NAME, OPTARG is unset, and a diagnostic message is printed.
当我在 非静音模式 中实现我的 getopts 函数时, /.statsrandomrun.sh -m
触发 ?
后缺少的参数被存储在 $Name
(在我的例子中 $opt
),$OPTARG
中没有内容。
所以执行了这几行代码:
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
生成 Invalid option: -
以及 内置诊断 消息。
为了得到我想要的结果,我应该使用静音模式,如
#!/bin/bash
while getopts ":m:s:xh" opt; do
#codes
done
通过在 OPTSTRING 中添加前面的冒号。