LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2009, 05:20 AM   #1
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Rep: Reputation: 15
grep for string and test if successfully or not


Hi guys I am using grep in a bash script to look for a string and i would like to check if the string was found

so my script goes a little some thing like this !!!

Code:
grep bboy /var/log/messages
echo $?
so assumed that since the string bboy was not in /var/log/messages I would get the value higher than 0 but wether or not the string is in /var/log/messages I still get the valure 0

why?
 
Old 02-17-2009, 05:28 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can use
grep bboy /var/log/messages && <do if found> || <do if not found>

The results from grep could also be used with an if statement.

Look at the manpage for grep to find what the return values are. 0 = found; 1 = not found; 2 = error. Posix states that an error returns a value greater than 1, so to be portable, you might want to use that as the test condition.
 
Old 02-17-2009, 05:33 AM   #3
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
Talking

Quote:
Originally Posted by jschiwal View Post
You can use
grep bboy /var/log/messages && <do if found> || <do if not found>

The results from grep could also be used with an if statement.

Look at the manpage for grep to find what the return values are. 0 = found; 1 = not found; 2 = error. Posix states that an error returns a value greater than 1, so to be portable, you might want to use that as the test condition.
thanks i dont know why i did not thing of that
 
Old 02-17-2009, 01:43 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

a faster way is the -q option of the grep command - it stops parsing the file on first match (and you aren't flooded with the grep output because there is none):
Code:
grep -q bboy /var/log/messages && <do if found> || <do if not found>
Jan
 
  


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 for a string in a file kaprasanna Linux - Newbie 3 01-06-2009 06:29 AM
ash test: is string A contained in string B chochem Programming 6 09-24-2008 06:59 AM
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 09:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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