Ok. You can try a test like this:
Code:
pattern=6
if ! grep -q $pattern file1
then
echo $pattern > another_file
echo "Pattern $pattern not found!"
fi
The -q option of grep stats for quiet mode, that prevents the output to be displayed on the terminal. The exclamation mark is the negation of the test: if the pattern is
not found, the echo statements are executed. Take in mind that each command in linux spits out an exit status. Usually the exit status for success is 0, which is interpreted by the shell as true. An exit code different than 0 (usually 1) is interpreted as false.
You can demonstrate it running the following:
Code:
$ cat file1
0
1
2
3
4
5
$ grep 6 file1
$ echo $?
1
The shell variable $? stores the exit status of the previous command.