【问题标题】:Why does grep work differently in Debian vs CentOs?为什么 grep 在 Debian 和 CentO 中的工作方式不同?
【发布时间】:2013-12-18 03:51:40
【问题描述】:

采用这个简单的shell函数...

function search (){
    grep -roiI --include $2 $1 . | sort -u
}

然后像这样使用它:

# search drop *.properties

在 CentOs 中,它会根据需要返回一个 grep 结果列表。但是,在 Debian 中,它将“*.properties”中的特殊字符解析为正则表达式,因此无法正确 grep'ing。为什么 Debian 不解析特殊字符和 CentO?

【问题讨论】:

  • 对了,你可以用sort -u代替sort | uniq
  • 好主意。我想知道为什么我被淘汰了。 :(~我现在很难过

标签: linux grep centos debian


【解决方案1】:

这听起来像是 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 参数括在双引号中。

【讨论】:

    【解决方案2】:

    也许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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2021-01-16
      • 2012-12-25
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多