【发布时间】:2021-10-07 04:01:09
【问题描述】:
在传递- 字符时使用以下getopt 时出现奇怪的错误。这是代码和输出的示例:
#!/bin/bash
function _echo()
{
msg="$1"
# A sample getopt option with only t as short parameter
# I put -q to silent error and use output based on getopt return status
options=$(getopt -q -o t -- "$@")
retval=$?
if [ ${retval} -ne 0 ]; then
echo "${msg} <--[this has error]"
else
echo "${msg} <--[this is ok]"
fi
echo ""
eval set -- "${options}"
}
# Calling function _echo() without passing any option (sample):
_echo "--" # OK
_echo "Hello" # OK
_echo "Hello--" # OK
_echo "-" # OK
_echo "---------------" # Error
_echo "---" # Error
_echo "--Hello" #Error
_echo "-Hello" #Error
输出:
-- <--[this is ok]
Hello <--[this is ok]
Hello-- <--[this is ok]
- <--[this is ok]
--------------- <--[this has error]
--- <--[this has error]
--Hello <--[this has error]
-Hello <--[this has error]
你可以看到上面传递给_echo函数的一些语句有错误,即使字符串被引用了?如果您想在 _echo 函数中传递长破折号 (-------),如何解决这个问题?
谢谢。
【问题讨论】: