Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-19-2013, 11:02 AM   #1
LQ Newbie
Registered: Jun 2013
Location: my house
Distribution: centos, ubuntu
Posts: 10

Rep: Reputation: Disabled
return value for find or grep for use as an if statement condition

I am trying to write a script using if statements but I am unclear as to what some linux commands return. for instance could I use the "find" command as the condition for an if statement as shown below?

 if [ $(find /mypath -name main.cpp) ]; then
(I want this to determine if the file exists and if it does then execute commands on the file)

does the find command return true if it finds the file and false if it doesn't?
I am wondering the same thing about the grep command. I read the man pages and it says "find" returns a 0 if all files are processed succesfully and other numbers if not. Does that mean it will return a 0 if it finds the file? or does that just mean it returns a 0 if the "find" command runs succesfully without running into problems?
Old 07-19-2013, 11:12 AM   #2
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
The program you show returns the text output from find inside the [].

The grep answer is you get 0,1 or 2. You can actually test this faster than posting the question here.

if grep root: /etc/passwd
    echo surprise
    echo the other thing
1 members found this post helpful.
Old 07-19-2013, 11:15 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Immediately after a command exits, its exit code is stored in the special variable "$?". echo that to see what you'll get, or test it in the usual ways.

But actually you generally don't need to use any kind of test here. The [..] test brackets are just a command like any other, and give an exit code, which is what the if/while/until constructs are actually working with. So you can replace it with just about any command if you want.

if grep -q 'foobar' file.txt ; then
    echo 'string found in file'
    echo 'string not found in file'
find, however, doesn't appear to give you the kind of exit code you want. It only gives a non-zero status if there are errors in the command. So for that you have to do something like capture the output in a variable/array and test that for the existence of a string.

Or actually, the way you have it written in the OP may do just as well, since the $(...) will be substituted with the text output of the command. [..] will exit successfully if it finds a text string (although it's better to use the explicit -n/-z test options). The only caveat is that you have to double quote the substitution or else it will probably error out on multi-word strings (too many arguments).

The bash/ksh [[..]] extended test doesn't have that problem, by the way, and so is recommended.

Last edited by David the H.; 07-19-2013 at 11:27 AM. Reason: added last part
1 members found this post helpful.
Old 07-19-2013, 11:45 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
May I also suggest that if you are looking for a file, then just test for it:
shopt -s globstar
shopt -s nullglob

check_file=( /mypath/**/main.cpp )

if [[ -e "$check_file" ]]
    echo "yep it's there :)"
    echo "nup, couldn't find it :("
1 members found this post helpful.


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to grep with certain condition ramsavi Programming 5 05-20-2013 06:11 AM
Condition in cp/ls | grep (regex, now I have two problems) Freddythunder Linux - Newbie 6 07-06-2012 09:39 AM
[SOLVED] less than condition in if statement not working as desired samasat Linux - Newbie 11 06-09-2012 06:33 PM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 01:38 AM
How to check grep status when find elements return 0 ahjiefreak Programming 18 12-18-2007 06:06 PM

All times are GMT -5. The time now is 01:04 PM.

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