If grep finds a term do something | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

If grep finds a term do something

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-09-2011
cokedude cokedude is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 2 November 2014, 2:38 AM EST
Posts: 407
Thanks: 28
Thanked 9 Times in 9 Posts
If grep finds a term do something

I would like to do something if grep finds the word nameserver. I was reading this thread so I tried it with a few changes.

http://www.unix.com/shell-programmin...ing-there.html

I am not seeing any output or receiving any error messages. Can someone tell me what is wrong with this shellscript?


Code:
#!/bin/bash
grep -q 'nameserver' resolv.conf
if [[ $? -eq 0 ]] ;
then
    # do stuff to file
    echo "nameserver found"
else
    echo "nameserver not found"
fi

Sponsored Links
    #2  
Old 09-09-2011
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts

Code:
grep -q 'nameserver' resolv.conf && echo "Pattern Found" || echo "Pattern Not Found"

Sponsored Links
    #3  
Old 09-09-2011
hfreyer hfreyer is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 18 March 2014, 9:53 PM EDT
Posts: 40
Thanks: 0
Thanked 6 Times in 6 Posts
For me it works. If file resolv.conf does not exist in the current directory, the response is:

Code:
grep: resolv.conf: No such file or directory
nameserver not found

If resolv.conf exists in the current directory and does not contain string nameserver:

Code:
nameserver not found

If resolv.conf exists in the current directory and contains string nameserver:

Code:
nameserver found

Are you calling the script from command line ?
Without any redirection ?
    #4  
Old 09-09-2011
dreipapier dreipapier is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 9 September 2011, 12:00 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
if  ! grep -q 'nameserver' resolv.conf then 
  echo "nameserver found"
else
  echo "nameserver not found"
fi


Last edited by Franklin52; 09-12-2011 at 08:27 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #5  
Old 09-11-2011
cokedude cokedude is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 2 November 2014, 2:38 AM EST
Posts: 407
Thanks: 28
Thanked 9 Times in 9 Posts
Quote:
Originally Posted by itkamaraj View Post
Code:
grep -q 'nameserver' resolv.conf && echo "Pattern Found" || echo "Pattern Not Found"

How exactly does that work? I remember seeing this also while I was looking.


Code:
[ -f /etc/passwd ] && echo "File exists" || echo "File does not exists"

Quote:
Originally Posted by hfreyer View Post
For me it works. If file resolv.conf does not exist in the current directory, the response is:

Code:
grep: resolv.conf: No such file or directory
nameserver not found

If resolv.conf exists in the current directory and does not contain string nameserver:

Code:
nameserver not found

If resolv.conf exists in the current directory and contains string nameserver:

Code:
nameserver found

Are you calling the script from command line ?
Without any redirection ?
I think I got some special character in their somehow. When I ran the file command on it, it wasn't a ASCII text file. When I typed it up fresh again it worked.
Sponsored Links
    #6  
Old 09-11-2011
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts
If the previous command is successful ( grep command ) then the below will execute


Code:
&& echo "Pattern Found"


If it is not successful, then the else block will execute


Code:
|| echo "Pattern Not Found"

check 3.2.3 section in the below link
Bash Reference Manual
Sponsored Links
    #7  
Old 09-11-2011
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Quote:
Originally Posted by dreipapier View Post
if ! grep -q 'nameserver' resolv.conf then
echo "nameserver found"
else
echo "nameserver not found"
fi
That use of ! is incorrect. The logical inversion of the exit status yields: if grep failed to find "nameserver" then echo "nameserver found".

Regards,
Alister
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search term and output term in desired field Raynon Shell Programming and Scripting 28 03-04-2007 02:34 AM
fixing the error message when grep doesn't finds the pattern. silver123 UNIX for Dummies Questions & Answers 1 03-03-2006 05:59 AM
Appending to filename a string of text grep finds HLee1981 Shell Programming and Scripting 3 09-06-2005 03:44 PM
Create a Term & Run chars on this Term the_tical Programming 1 08-12-2003 10:18 PM
PS finds a ghost? jguirao UNIX for Dummies Questions & Answers 10 03-02-2001 05:48 PM



All times are GMT -4. The time now is 03:22 AM.