|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
grep -q 'nameserver' resolv.conf && echo "Pattern Found" || echo "Pattern Not Found" |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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 07:27 AM.. Reason: Please use code tags for code and data samples, thank you |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Quote:
Code:
[ -f /etc/passwd ] && echo "File exists" || echo "File does not exists" Quote:
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
Regards, Alister |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 01:34 AM |
| fixing the error message when grep doesn't finds the pattern. | silver123 | UNIX for Dummies Questions & Answers | 1 | 03-03-2006 04:59 AM |
| Appending to filename a string of text grep finds | HLee1981 | Shell Programming and Scripting | 3 | 09-06-2005 02:44 PM |
| Create a Term & Run chars on this Term | the_tical | Programming | 1 | 08-12-2003 09:18 PM |
| PS finds a ghost? | jguirao | UNIX for Dummies Questions & Answers | 10 | 03-02-2001 04:48 PM |
|
|