【问题标题】:How to tell if the output of the "find" command is empty?如何判断“find”命令的输出是否为空?
【发布时间】:2011-09-25 12:21:06
【问题描述】:

如果输出为空,我想返回退出状态 0,否则返回 1:

find /this/is/a/path/ -name core.*

【问题讨论】:

标签: bash pipe


【解决方案1】:

当你说你希望它返回一个特定的数字时,你指的是退出状态吗?如果是这样:

[[ -z `find /this/is/a/path/ -name core.*` ]]

而且由于您只关心是/否响应,您可能希望将您的发现更改为:

[[ -z `find /this/is/a/path/ -name core.* -print -quit` ]]

在找到第一个核心文件后将停止。否则,如果根目录很大,查找可能需要一段时间。

【讨论】:

  • 如果输出为非空则返回 0,如果为空则不返回。
  • 糟糕。逻辑颠倒了。上面更正了。亚历克斯对-z 是正确的
【解决方案2】:

这是我的版本。 :)

[ -z "$(find /this/is/a/path/ -name 'core.*')" ] && true

为简洁而编辑:

[ -z "$(find /this/is/a/path/ -name 'core.*')" ]

【讨论】:

  • 呵呵,我在想,“设置退出状态。”但是,是的,呃,测试已经这样做了,完全没有必要,很好。
【解决方案3】:

可能有很多变种,但这是一个:

test $(find /this/is/a/path/ -name core.* | wc -c) -eq 0

【讨论】:

    【解决方案4】:

    也许是这样

    find /this/is/a/path/ -name 'core.*' | read
    

    【讨论】:

      【解决方案5】:

      我正在使用这个:

      if test $(find $path -name value | wc -c) -eq 0
          then
           echo "has no value"
           exit
      else
         echo "perfect !!!"
      fi 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-25
        • 2019-03-20
        • 2018-08-27
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        相关资源
        最近更新 更多