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