开发者

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

开发者 https://www.devze.com 2023-03-16 02:34 出处:网络
I want to return an exit status of 0 if the output is empty开发者_C百科 and 1 otherwise: find /this/is/a/path/ -name core.*

I want to return an exit status of 0 if the output is empty开发者_C百科 and 1 otherwise:

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


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.


Here's my version. :)

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

Edited for brevity:

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


There are probably many variants, but this is one:

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


Perhaps this

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


I am using this:

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

精彩评论

暂无评论...
验证码 换一张
取 消