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]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux