LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-19-2013, 11:02 AM   #1
IneptCoder
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?

Code:
 if [ $(find /mypath -name main.cpp) ]; then
command
command 
fi
(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
linosaurusroot
Member
 
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.

Code:
if grep root: /etc/passwd
then
    echo surprise
else
    echo the other thing
fi
 
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.

Code:
if grep -q 'foobar' file.txt ; then
    echo 'string found in file'
else
    echo 'string not found in file'
fi
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.

http://mywiki.wooledge.org/BashFAQ/031

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
grail
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:
Code:
shopt -s globstar
shopt -s nullglob

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

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


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 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
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration