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 and shell scripting languages here.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-20-2008
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
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.
  #2 (permalink)  
Old 03-20-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753

Code:
cd ${PATH}

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

exit 0

  #3 (permalink)  
Old 03-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
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.
  #4 (permalink)  
Old 03-20-2008
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
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.
  #5 (permalink)  
Old 03-21-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557

Code:
grep -l "test" test* | xargs -i rm "{}" && ./script.sh

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0