bash 如何判断“find”命令的输出是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6527227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:16:02  来源:igfitidea点击:

How to tell if the output of the "find" command is empty?

bashpipe

提问by cstack

I want to return an exit status of 0 if the output is empty and 1 otherwise:

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

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

回答by Rob Davis

When you say you want it to return a particular number, are you referring to the exit status? If so:

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

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

And since you only care about a yes/no response, you may want to change your find to this:

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

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

which will stop after the first core file found. Without that, if the root directory is large, the find could take a while.

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

回答by Alex Howansky

Here's my version. :)

这是我的版本。:)

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

Edited for brevity:

为简洁起见编辑:

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

回答by Peter Eisentraut

There are probably many variants, but this is one:

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

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

回答by Steven Penny

Perhaps this

也许这

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

回答by Thomas Rassloff

I am using this:

我正在使用这个:

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