Bash 如何避免扩展“eval集合”中的单引号文本&引用$@";`对于getopt?
我使用传统模式通过getopt执行参数解码:Bash 如何避免扩展“eval集合”中的单引号文本&引用$@";`对于getopt?,bash,shell,wildcard,getopt,quoting,Bash,Shell,Wildcard,Getopt,Quoting,我使用传统模式通过getopt执行参数解码: function mytest { eval set -- `getopt --options h --long help -- "$@"` echo "1:$1 2:$2" } 但当我传递一个带引号的字符串时,它实际上是展开的,例如: $ mytest 'x * z' 1:-- 2:<list of files from current dir> 如何按预期执行评估?因为您在eval中使用,所以我看不到除禁用globs之外的
function mytest {
eval set -- `getopt --options h --long help -- "$@"`
echo "1:$1 2:$2"
}
但当我传递一个带引号的字符串时,它实际上是展开的,例如:
$ mytest 'x * z'
1:-- 2:<list of files from current dir>
如何按预期执行评估?因为您在
eval
中使用,所以我看不到除禁用globs之外的其他选项(扩展发生在命令实际运行之前),即:
引用您的扩展以防止全球化:
function mytest {
eval set -- "`getopt --options h --long help -- "$@"`"
echo "1:$1 2:$2"
}
请注意,
set-h
(set-o hashall
)与set--help
不同,这似乎不是读取可选参数Shello的传统方式!对不起,我不明白这个评论-h
/--help
不是getopt
命令的选项,而是函数的选项。换句话说,例如,使用eval set--`getopt--options u--long-uselessoption--“$@”`
仍然会得到相同的结果。这也是由感谢自动建议的!您能否详细说明一下,没有您的建议,mytest'*z'
没有展开,而mytest'x*z'
是展开的原因?@Marcusfoo*
匹配所有以foo
开头的文件<代码>'*匹配以单引号开头的所有文件。你可能没有。
mytest ()
{
set -f # disable file name generation (globbing).
eval set -- $(getopt --options h --long help -- "${@}")
echo "1:${1} 2:${2}"
set +f
}
$ mytest x * z
1:-- 2:x
$ mytest 'x * z'
1:-- 2:x * z
$ mytest ./*
1:-- 2:./config-err-reCeGT
$ mytest "./*"
1:-- 2:./*
function mytest {
eval set -- "`getopt --options h --long help -- "$@"`"
echo "1:$1 2:$2"
}