LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 10: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, 10:12 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 10:15 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 10:27 AM. Reason: added last part
 
1 members found this post helpful.
Old 07-19-2013, 10:45 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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



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 05:11 AM
Condition in cp/ls | grep (regex, now I have two problems) Freddythunder Linux - Newbie 6 07-06-2012 08:39 AM
[SOLVED] less than condition in if statement not working as desired samasat Linux - Newbie 11 06-09-2012 05:33 PM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
How to check grep status when find elements return 0 ahjiefreak Programming 18 12-18-2007 05:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:03 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
Open Source Consulting | Domain Registration