The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
if test statement skooly5 UNIX for Dummies Questions & Answers 1 04-13-2008 04:32 PM
TAHI Test Suite 3.0.13 (IPv6 Conformance Test Tool branch) iBot Software Releases - RSS News 0 04-06-2008 09:20 AM
Unix grep/test command netmaster UNIX for Dummies Questions & Answers 1 10-09-2007 02:29 AM
Keithley Introduces Linux-Based RF Parametric Test Systems - Test and Measurement.com iBot UNIX and Linux RSS News 0 07-23-2007 07:30 AM
Using grep in if statement chiru_h Shell Programming and Scripting 3 09-12-2006 07:00 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-20-2008
Registered User
 

Join Date: Mar 2008
Posts: 30
Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script.

Anyways, here's my code:

Code:
cd ${PATH}

if [ 'grep SOME_STRING $PATH/$LOGFILE' ]
then
        rm ${FILE}       
        ./anotherScript
else
        exit 1
fi
exit 1
Anyways, it's a pretty simple script that is supposed to search for the string SOME_STRING in a log file, and if it finds it, then perform the next two steps of removing a file and running another script. If it doesn't find the string, then the script just exits. I tested it, it found the string, and it worked. But then I double checked and tested it when I knew the string wasn't present in the log file, and it still ran the script, instead of exiting.
Reply With Quote
Forum Sponsor
  #2  
Old 03-20-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Code:
cd ${PATH}

if [ $(grep -c SOME_STRING $PATH/$LOGFILE) -ne 0 ]
then
        rm ${FILE}       
        ./anotherScript
else
        exit 1
fi

exit 0
Reply With Quote
  #3  
Old 03-20-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
For really large files all you want is a yes/no instead of reading thru the whole file.
Code:
cd ${PATH}

grep -q SOME_STRING $PATH/$LOGFILE
if [ $? -eq 0 ]
then
        rm ${FILE}       
        ./anotherScript
else
        exit 1
fi

exit 0
grep -q exits as soon as it gets a hit.

Otherwise shamrock's code is just fine.
Reply With Quote
  #4  
Old 03-20-2008
Registered User
 

Join Date: Mar 2008
Posts: 30
Thanks for all your help. Got it the first way, tomorrow I'll try it the second way b/c these files can get quite big.

Thanks again, I'm sure i'll be on here more often now.
Reply With Quote
  #5  
Old 03-20-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Code:
grep -l "test" test* | xargs -i rm "{}" && ./script.sh
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0