Using grep in a test/if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep in a test/if statement
# 1  
Old 03-20-2008
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  
Old 03-20-2008
Code:
cd ${PATH}

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

exit 0

# 3  
Old 03-20-2008
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  
Old 03-20-2008
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  
Old 03-21-2008
Code:
grep -l "test" test* | xargs -i rm "{}" && ./script.sh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using grep with test and without using [[ ]]

As an exercise, I'm trying to re-write this code without the compound square brackets, using grep and test. Need to know what to do about the "equal-tilde". #!/bin/bash # test-integer2: evaluate the value of an integer. INT=-5 if +$ ]]; then if ; then echo "INT is zero." else if ; then... (17 Replies)
Discussion started by: Xubuntu56
17 Replies

2. Shell Programming and Scripting

Using GREP in IF Statement

Hello All, I have 2 different pieces of code, I am confused why the Code1 is giving me the correct result where as the Code2 is not giving me correct result. It gives me always result as "Failure" irrespective of the "ERROR" word exists in logfile or not. may I know the reason why? I am using Bash... (17 Replies)
Discussion started by: Ariean
17 Replies

3. Shell Programming and Scripting

If statement test against number range [0-9]

Is it possible to test against a varible within a ranges in a if statement. ex. if ];then echo "not in range" else echo "number within range" fi (8 Replies)
Discussion started by: leemalloy
8 Replies

4. Shell Programming and Scripting

Help with test statement

Hello, I am trying to build a test statement but I can't make it work I want to rearrange some fields, so if my "$cfg" variable contains a string ending with .log (*.log) I want to move it in another field. Any help will be much appreciated! Thank you Shell:sh if then log="${cfg}"... (9 Replies)
Discussion started by: drbiloukos
9 Replies

5. Shell Programming and Scripting

string test in IF statement

How do I test multiple words in a string test like below: if ] then print "You entered $TBS name.\n" else print "You entered an incorrect response.\n" fi This test does not work. I have tried different syntax versions. How does this work? And is there a better way to do it? ... (10 Replies)
Discussion started by: djehresmann
10 Replies

6. Shell Programming and Scripting

Perl - automating if statement test

Hello all, I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file. There are approximately 200 different input files. Each has... (3 Replies)
Discussion started by: Galt
3 Replies

7. Shell Programming and Scripting

Using grep inside a test

Hi, I want to use grep inside a test statement, but I am getting an error message. Two variables testvarNum=5 testvarNonNum=x echo $testvarNum | grep * The result of this is as follows: 5 However, when I try the following (i.e. to test if the variable is numeric or non-numeric):... (3 Replies)
Discussion started by: dkieran
3 Replies

8. Shell Programming and Scripting

Grep statement

Hi All, Please can somebody advise that if I want to search a pattern xyz the grep command should only select xyz and not any other pattern containing xyz (ex abxyzcd) Regards (1 Reply)
Discussion started by: Shazin
1 Replies

9. UNIX for Dummies Questions & Answers

if test statement

Can you use an if statement after an else? example if then echo "word" else if then echo "word" (1 Reply)
Discussion started by: skooly5
1 Replies

10. Shell Programming and Scripting

Using grep in if statement

Can somebody please guide me towards right syntax: #!/bin/ksh if i = $(grep $NAME filename) echo "Name Found" else echo " Name not Found" fi I need to grep for $NAME in the file, and if it returns false, execute a series of commands and if true, exit out. The above is not the right... (3 Replies)
Discussion started by: chiru_h
3 Replies
Login or Register to Ask a Question