LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 01-13-2004, 10:45 AM   #1
bobbyr
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Rep: Reputation: 0
how to grep within a script, and test result?


I want to grep for a record in a text file from within a ksh script. I want to pass a variable.

grep `$checkpolicy $DONELIST`

If the result is successfull skip the next piece of code, (which sends an email), and continue. The email will have already been sent on the previous pass.

How do i test for the result, what parameter will the grep return?

Thanks.
 
Old 01-13-2004, 11:06 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
grep returns a 0 on a find. You can check the return code with the $? variable.
Code:
#!/bin/ksh

checkpolicy=hello
DONELIST="file1 file2 file3"

grep $checkpolicy $DONELIST

if [ $? -eq 0 ]; then
   echo "Hey, found it"
else
   echo "No such luck"
fi
 
Old 01-13-2004, 11:53 AM   #3
bobbyr
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0
Smile

Thanks for the quick reply, it is greatly appreciated. I am still trying to get a handle on shell scripting, very new to it.

Is this a good forum for shell scripting type of questions?

Bob
 
Old 01-13-2004, 11:57 AM   #4
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Quote:
Originally posted by bobbyr
Is this a good forum for shell scripting type of questions?
It's definitely better than nothing I know that myself and a few others are pretty familiar with shell scripting, so feel free to ask anytime.
 
Old 01-13-2004, 12:11 PM   #5
bobbyr
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0
thanks!

bob
 
  


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
Using find and grep - how to exclude a result ? graziano1968 Linux - General 13 12-04-2010 10:02 PM
Need to assign result of grep to var in PERL amytys Programming 1 09-23-2004 06:25 PM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 05:28 AM
multiline grep result > multiple variable? mikshaw Linux - Software 5 12-25-2003 10:13 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM


All times are GMT -5. The time now is 04:46 AM.

Main Menu
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