![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | 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. |
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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|