LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 03-15-2005, 02:22 AM   #1
mufy
Member
 
Registered: Oct 2004
Location: Kuwait
Distribution: Currently - AIX | Previously - RHEL 4 ES, FC 10
Posts: 206
Blog Entries: 4

Rep: Reputation: 30
determine whether 'find' cmd found the file or not


I am writing a short shell script wherein I would like to continuously search for the presence of a particular file in a particular folder.

This is what I'd like to know; how do I determine the status of the 'find' command so that I can execute a loop till that file has been found? -- Mufy
 
Old 03-15-2005, 03:42 AM   #2
overlord73
Member
 
Registered: Apr 2004
Location: ..where no life dwells..
Distribution: RH,FC/SuSE/Debian/HPUX/OSX
Posts: 511

Rep: Reputation: 30
to determine the exit status of a command us the $? variable:

QUOTE]# find / ......
# echo $?[/QUOTE]

If return value is zero (0), command is successful.
If return value is nonzero, command is not successful .[
 
Old 03-15-2005, 03:52 AM   #3
Darin
Senior Member
 
Registered: Jan 2003
Location: Portland, OR USA
Distribution: Slackware, SLAX, Gentoo, RH/Fedora
Posts: 1,024

Rep: Reputation: 45
This doesn't use the find command, but:

Code:
#!/bin/bash

if [ ! -r /path_to/file ]; then
# if this file can't be read then do something here like
sleep 5
fi
For commands, you can run them as a conditional argument and they will come out TRUE if they don't produce any errors:
Code:
#!/bin/bash

if find /path_to/some.file; then
echo I found the file
fi

if ! find /path_to/some.file; then
echo I didn't find the file
fi
Edit: jinx overlord73

Last edited by Darin; 03-15-2005 at 03:53 AM.
 
Old 03-15-2005, 08:30 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
As far as I know, find does not give an error status if no files are found. Depending on exactly what you want to do, I'm sure there's a better solutio, though.

Yves.
 
Old 03-15-2005, 08:54 AM   #5
Darin
Senior Member
 
Registered: Jan 2003
Location: Portland, OR USA
Distribution: Slackware, SLAX, Gentoo, RH/Fedora
Posts: 1,024

Rep: Reputation: 45
Quote:
Originally posted by theYinYeti
As far as I know, find does not give an error status if no files are found. Depending on exactly what you want to do, I'm sure there's a better solutio, though.

Yves.
AFAIK every command returns, or at least should, an error code on exit that can be used as a comparison operator (or as overlord73 stated, shown with $?). I tested those mini-scripts I posted above and they showed different results before and after touching the appropriate test file. I like the if [! -r file] (not readable) though, since I got that from a Slackware startup script
 
Old 03-15-2005, 11:14 AM   #6
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 30
I'm not sure that looking to the error status would be a good strategy. What would it happen if find cannot access a directory? (for instance, if the user running the program has not permissions) It could happen that the file is found, but an error occuried.

I don't know exactly what "find" returns. Just wanted to point out that you should be careful.
 
Old 03-16-2005, 07:18 AM   #7
overlord73
Member
 
Registered: Apr 2004
Location: ..where no life dwells..
Distribution: RH,FC/SuSE/Debian/HPUX/OSX
Posts: 511

Rep: Reputation: 30
if "find" cannot access a dir it returns "1".
try as user:

Code:
find /root/ -name filename
Code:
echo $?
 
Old 03-16-2005, 08:24 AM   #8
jmercier
LQ Newbie
 
Registered: Nov 2004
Location: Dallas, TX
Distribution: Suse 10
Posts: 25

Rep: Reputation: 15
..

You can always just test to see if it returns anything.

Code:
find <dir> -name <filename> | wc -l
Justin
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sudo command not working startproc cmd not found Imajica21 Linux - General 2 04-14-2005 02:22 PM
iostat cmd not found karthic Linux - Enterprise 2 03-22-2005 06:04 AM
Need cmd to know if the necessary dir/file exists kornerr Linux - General 2 02-07-2005 10:49 AM
installpkg cmd not found Hectic Slackware 2 10-13-2004 08:31 AM
CSH: "cmd >& file" or "cmd </dev/null >& file" stefanlasiewski Programming 1 09-08-2003 05:19 PM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration