test egrep not found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test egrep not found
# 1  
Old 05-16-2008
Network test egrep not found

Hi,

I want to return failure from my script if a string is NOT found in a file, but I can't work out how to negate my "if". At the moment I have :

if (egrep -i 'search string' filetosearch); then
echo "found"
else
return 2
fi

How can I get rid of the echo bit and just test for "string not found" ? I already know I can use egrep -v to return lines not containing the string, but I actually just want to fail if the search string is not in the file anywhere. Thanks.
# 2  
Old 05-16-2008
Code:
#! /usr/bin/ksh
egrep -i "string" file_name
if [ $? -eq 0 ]
then
echo "Found"
else
return 2
fi

# 3  
Old 05-17-2008
That's a Useless Use of Test $?. if already examines the exit code $? from the command it executes, so it's simpler and more straightforward to do

Code:
if egrep -i "string" file_name
then
    echo Found
else
    return $?
fi

To negate an if, use an exclamation mark.

Code:
if ! egrep -i "string" file_name
then
    return $?
fi

There's another shorthand you should know about: the "or" connective. It executes the second command if the first command fails.

Code:
egrep -i "string" file_name >/dev/null || return $?

There is also && "and" which operates the other way around.

(I added redirection to /dev/null as egrep is used simply for its return value here. You don't want it to actually print any matches. If your egrep has the -q option, you could use that too.)
# 4  
Old 05-19-2008
Power

Great, thanks. I had tried ! already as this seemed pretty obvious but it didnt seem to work .. but now it is ! Thanks guys ...
# 5  
Old 05-19-2008
the best ....
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using egrep to output expressions which are not found

Hi, Im using the below command to search a file for multiple expressions if the 4th expression doesnt exist the command simply lists what it can find and ignores what it cant. Is there any way to get the command to output an error or a message if it cant find the 4th expression to a file? ... (16 Replies)
Discussion started by: 02JayJay02
16 Replies

2. Shell Programming and Scripting

Prefixing test case methods with letter 'test'

Hi, I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
Discussion started by: royalibrahim
5 Replies

3. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

7. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies
Login or Register to Ask a Question