Re: Force exit status if file not found

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

 



2011/3/7 Kevin Martin <kevintm@xxxxxxxxxxxxx>:
>
>
> 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

Good suggestion

>
> 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?

Perhaps find e try to make some kind of pathname expansion and failing
to do, so it exits with status non-zero.

In manpage find says:

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

So, I guess that exit non-zero for non-existing files it's a bash
thing. It could be nice if find exit with status non-zero whenever
don't find a file into search path.




>
>
> 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
>



-- 
--
Sergio Belkin  http://www.sergiobelkin.com
Watch More TV http://sebelk.blogspot.com
LPIC-2 Certified - http://www.lpi.org
-- 
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