How to use grep on Linux to report in a verbose way whether a string is present in a file?
Solution 1:
Just check the exit code of grep
. -q
makes it silent, !
negates the exit code:
if ! grep -q 'ack_enabled not defined' all_defs.txt ; then
echo Not found.
fi
Solution 2:
More vebose one-liner
The following one-liner is even more verbose:
grep -q 'ack_enabled not defined' all_defs.txt && echo 'string found' || echo 'string not found'