拿这个简单的外壳函数...
function search (){
grep -roiI --include $2 $1 . | sort -u
}
然后像这样使用它:
# search drop *.properties
在 CentOs 中,它将根据需要运行,返回 grep'd 结果的列表。但是,在 Debian 中,它将 "*.properties" 中的特殊字符解析为正则表达式,因此无法正确进行 grep'ing。为什么 Debian 不解析特殊的字符和 CentO?
听起来像是 nullglob
shell 选项的不同设置,该选项控制当您使用 glob(带有通配符的东西)并且没有与该 glob 匹配的文件时会发生什么。 打开 nullglob 后,这会将".properties"视为文件列表,即使这是一个空列表,在 nullglob 关闭的情况下,如果".properties"与任何文件不匹配,也会将其视为字符串。 您可以尝试使用 shopt -u nullglob
禁用 nullglob,然后使用 shopt -s nullglob
重新打开它。
但是,在这种情况下,当您不希望将 *.properties 视为 glob 并且希望将此字符串直接传递到脚本中时,您应该将 * 转义为 search drop *.properties
,或者您应该用双引号或单引号引用字符串:search drop '*.properties'
。 同样,在搜索脚本中,您应该将$2
、$1
参数括在双 qoutes 中。
也许grep
不是问题。这可能是外壳膨胀问题。
在 bash:
Bash scans each word for the characters ‘*’, ‘?’, and ‘[’. If one of these
characters appears, then the word is regarded as a pattern, and replaced with
an alphabetically sorted list of file names matching the pattern.