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.
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
To negate an if, use an exclamation mark.
There's another shorthand you should know about: the "or" connective. It executes the second command if the first command fails.
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.)
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)
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)
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)
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)
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)
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)