when I perform

  find /tmp  -name something 

find command not find the something word under /tmp

  echo $?

I get $?=0

it's OK

but how to enable Exit status diff then 0 when find command not find the something word?

link|improve this question

69% accept rate
Please don't cross-post. – Dennis Williamson Jan 23 '11 at 17:13
feedback

3 Answers

up vote 1 down vote accepted

Here is a one-liner that I believe does what you want:

find /tmp -name something | egrep '.*'

The return status will be 0 when something is found, and non-zero otherwise.

If you also need to capture the output of find for further processing, then SvenW's answer has covered that.

link|improve this answer
feedback

It's not possible. Find returns 0 if it exits successfully, even if it didn't find a file (which is a correct result not indicating an error when the file indeed doesn't exist).

To quote the find manpage

EXIT STATUS

find exits with status 0 if all files are processed successfully, greater than 0 if errors occur. This is deliberately a very broad description, but if the return value is non-zero, you should not rely on the correctness of the results of find.

Depending on what you want to achieve you could try to let find -print the filename and test against it's output:

#!/bin/bash
MYVAR=`find . -name "something" -print`
if [ -z "$MYVAR" ]; then
    echo "Notfound"
else
   echo $MYVAR
fi
link|improve this answer
feedback

It is not only find that returns the exit status codes as zero when it successful. In unix what ever the command you execute, if its succeeds then it returns the exit status as zero.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.