26

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

find /this/is/a/path/ -name core.*
pfrenssen
  • 5,498
  • 1
  • 22
  • 16
cstack
  • 2,152
  • 6
  • 28
  • 47

5 Answers5

27

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.

Rob Davis
  • 15,597
  • 5
  • 45
  • 49
6

Here's my version. :)

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

Edited for brevity:

[ -z "$(find /this/is/a/path/ -name 'core.*')" ]
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Heh, I was thinking, "To set the exit status." But yeah, duh, the test already does that, it's totally unnecessary, good catch. – Alex Howansky Jun 29 '11 at 21:42
3

There are probably many variants, but this is one:

test $(find /this/is/a/path/ -name core.* | wc -c) -eq 0
Peter Eisentraut
  • 35,221
  • 12
  • 85
  • 90
2

Perhaps this

find /this/is/a/path/ -name 'core.*' | read
Zombo
  • 1
  • 62
  • 391
  • 407
2

I am using this:

if test $(find $path -name value | wc -c) -eq 0
    then
     echo "has no value"
     exit
else
   echo "perfect !!!"
fi 
dbc
  • 104,963
  • 20
  • 228
  • 340
Rossi
  • 21
  • 1
  • 4