我正在为我的 bourne 脚本测试 getopt,我发现了一些非常有趣的结果。
#!/bin/sh
params=$(getopt lLo:p:t: "$*")
exit 0
如果我执行以下操作:
./myscript -z blah -o legal -p another_legal
正如预期的那样,我会得到:
getopt: illegal option -- z
getopt: illegal option --
getopt: illegal option -- b
getopt: illegal option -- l
getopt: illegal option -- a
getopt: illegal option -- h
但是如果我这样做:
./myscript -o legal -p another_legal -z blah
getopt
没有捕捉到这个。有什么想法吗?
最佳答案
"$*"
将您的函数接收到的所有参数作为单个参数传递给 getopt
。所以你实际上是在运行 getopt lLo:p:t: "-o legal -p another..."
你想要 "$@"
代替。
关于linux - 带有 getopt 的命令行参数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17351132/