Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-13-2004, 11:45 AM   #1
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?

Old 01-13-2004, 12:06 PM   #2
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.

DONELIST="file1 file2 file3"

grep $checkpolicy $DONELIST

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

Original Poster
Rep: Reputation: 0

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?

Old 01-13-2004, 12:57 PM   #4
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
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, 01:11 PM   #5
LQ Newbie
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0



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

All times are GMT -5. The time now is 07:26 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