FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Redhat > Fedora User

 
 
LinkBack Thread Tools
 
Old 03-07-2011, 09:08 PM
Sergio Belkin
 
Default Force exit status if file not found

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!
--
--
Sergio Belkin *http://www.sergiobelkin.com
Watch More TV http://sebelk.blogspot.com
LPIC-2 Certified - http://www.lpi.org
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 03-07-2011, 09:34 PM
Kevin Martin
 
Default Force exit status if file not found

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@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 03-07-2011, 09:42 PM
Mikkel
 
Default Force exit status if file not found

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!
You may want to look at the test command. (man test) It is often
used in conjunction with the if command. You can also look at Bash's
built in [ command.

Mikkel
--

Do not meddle in the affairs of dragons,
for thou art crunchy and taste good with Ketchup!

--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 03-07-2011, 11:59 PM
Sergio Belkin
 
Default Force exit status if file not found

2011/3/7 Mikkel <mikkel@infinity-ltd.com>:
> 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!
> You may want to look at the test command. (man test) It is often
> used in conjunction with the if command. You can also look at Bash's
> built in [ command.

Of course, I know about test command
But test command is useful if you know the complete file path.

I've found an easier way in short something like:

cd $i && ls -R | egrep -q "^configure.ac$|configure.in" && ls -R |
egrep -q "^Makefile.am$|Makefile.in"

HTH

>
> Mikkel
> --
>
> *Do not meddle in the affairs of dragons,
> for thou art crunchy and taste good with Ketchup!
>
>
>
>



--
--
Sergio Belkin *http://www.sergiobelkin.com
Watch More TV http://sebelk.blogspot.com
LPIC-2 Certified - http://www.lpi.org
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 03-08-2011, 12:48 AM
Sergio Belkin
 
Default Force exit status if file not found

2011/3/7 Kevin Martin <kevintm@ameritech.net>:
>
>
> 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@lists.fedoraproject.org
> 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@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 03-10-2011, 11:22 PM
Cameron Simpson
 
Default Force exit status if file not found

On 07Mar2011 16:34, Kevin Martin <kevintm@ameritech.net> wrote:
| 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?

No, it just means that find has successfully searched your directory and
encountered no errors. Something like a directory it could not search
etc would evince a non-zero status.

One approach might be like this:

find the-dir -name e >find.output
if [ -s find.output ]
then
echo file found
else
echo file no found
fi

Of course, you will often want to check that exactly one file was found,
etc. For example:

nfiles=`wc -l <find.output`
case $nfiles in
0) echo nothing found ;;
1) the_file=$(<find.output)
echo found $the_file
;;
*) echo $nfiles files found
;;
esac

Cheers,
--
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

We now return you to the previously scheduled counter-steering flame-fest
and under-clothing auction, after a few words about your sponsor, the DoD.
- Denis McKeon <galway@chtm.eece.unm.edu>
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 

Thread Tools




All times are GMT. The time now is 03:32 AM.

VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org