Re: Force exit status if file not found

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 03/07/2011 04:08 PM, Sergio Belkin wrote:
> Hi,
> I am writing a script that it searchs for a file. I'd want to exit
> with status non-zero if the file is not found. But find cannot do
> that.
> find command only exit with status non-zero one file is not processed
> successfully.
>
> locate command con do that, but it cannot find a file from a given directory.
>
> How can I do it?
>
> Thanks in advance!

I find that if I'm in a directory and do a "find <filename>" and it fails the exit code is 1 (re: $? = 1).  So if I'm in a directory
that has files a b c and d and do "find a" I get an exit code of 0 (success) but if I do a "find e" I get exit code of 1 (failure)
along with a failure message.  So in your script you could do:

#!/bin/bash

cd <somedir>
find <filename> 2> /dev/null
if [ "$?" != "0" ]
 then
   exit 1
fi

Strangely enough, if I'm in a directory and do a "find . -name e" (where e doesn't exist) I get *no* failed message and an exit code
of 0.  I find that a bit odd as I would think that "find e" and "find . -name e" would be the same thing.  Perhaps that's something
to do with the bash shell?


kevin
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux