Shell script: getopt: --) 是什么意思?
Shell script: getopt: what does --) mean?
我正在编写一个 shell 脚本来处理参数列表,我使用了 getopt
。
但是我发现它没有按预期工作。
我设法将错误缩小到这些行:
case "" in
.....
--)
shift
break
;;
我可以看到 --)
总是与 case
匹配,无论我提供什么参数(例如 ./run -r -d
或 ./run
)。我不确定这个“--
”是什么意思或应该做什么
我用谷歌搜索了很多,但 none 结果解释了这个“--
”的意思。
我的 shell 脚本如下,我可以看到 echo "in --)"
总是打印。我不确定这个 in --)
是什么意思
OPTIONS=di:r:v
LONGOPTIONS=debug,inputdir:,random:,verbose
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "[=12=]" -- "$@")
if [[ $? -ne 0 ]]; then
echo "I’m sorry, `getopt` failed in this environment."
exit 2
fi
eval set -- "$PARSED"
echo "$#"
while true; do
case "" in
-d|--debug)
d=y
shift
;;
-i|--inputdir)
inputFolder=y
folder=""
shift 2
;;
-v|--verbose)
v=y
shift
;;
-r|--random)
randNumFile=""
shift 2
;;
--)
echo "in --)" #this echo always prints
shift
break
;;
*)
echo "arguments error"
exit
;;
esac
done
echo "$#"
这里是man getopt
:
Normally, no non-option parameters output is generated until all options and their arguments have been generated. Then '--' is generated as a single parameter, and after it the non-option parameters in the order they were found, each as a separate parameter.
换句话说,getopt
在所有选项后生成一个 --
,这样您就知道何时停止循环它们。 --
之后的所有单词都应被视为 non-option 个参数,即使它们以破折号开头。
我正在编写一个 shell 脚本来处理参数列表,我使用了 getopt
。
但是我发现它没有按预期工作。
我设法将错误缩小到这些行:
case "" in
.....
--)
shift
break
;;
我可以看到 --)
总是与 case
匹配,无论我提供什么参数(例如 ./run -r -d
或 ./run
)。我不确定这个“--
”是什么意思或应该做什么
我用谷歌搜索了很多,但 none 结果解释了这个“--
”的意思。
我的 shell 脚本如下,我可以看到 echo "in --)"
总是打印。我不确定这个 in --)
OPTIONS=di:r:v
LONGOPTIONS=debug,inputdir:,random:,verbose
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "[=12=]" -- "$@")
if [[ $? -ne 0 ]]; then
echo "I’m sorry, `getopt` failed in this environment."
exit 2
fi
eval set -- "$PARSED"
echo "$#"
while true; do
case "" in
-d|--debug)
d=y
shift
;;
-i|--inputdir)
inputFolder=y
folder=""
shift 2
;;
-v|--verbose)
v=y
shift
;;
-r|--random)
randNumFile=""
shift 2
;;
--)
echo "in --)" #this echo always prints
shift
break
;;
*)
echo "arguments error"
exit
;;
esac
done
echo "$#"
这里是man getopt
:
Normally, no non-option parameters output is generated until all options and their arguments have been generated. Then '--' is generated as a single parameter, and after it the non-option parameters in the order they were found, each as a separate parameter.
换句话说,getopt
在所有选项后生成一个 --
,这样您就知道何时停止循环它们。 --
之后的所有单词都应被视为 non-option 个参数,即使它们以破折号开头。