if grep -q "$D" testfile
then
echo "there"
else
echo "not there"
fi
You don't need to cat to grep; grep will read the file. You also don't want to test the output string like that. Test the return code from grep which will be 'success' if the pattern was matched.
It is hard to guess what you intended from faulty code. Please describe the condition in words.
Things to bear in mind.
Variables enclosed by single quote characters are not expanded.
The "grep" command does not recognise "|" as meaning "or". The "egrep" command does.
A command sequence in an "if" statement does not execute automatically. It needs either backticks or $( ) command syntax.
"if" waits a command, and "[" is just a command. Really this is a synonym for "test". When command executes it returns exit code. If exit code == 0 then "if" counts it as true condition and executes commands in its body.
When you write, for example
if [ -f 'some_file' ]; then ...
really you run the command:
if test -f 'some_file' ; then ...
In case of grep - this command returns 0 if it finds matches or non-0 if it's not. This is perfect condition for "if".
Excuse me if something is unclear. I'm working on my English.