There are many errors in your script. First attempt?
Here is a working version:
Code:
#!/bin/bash
> matches.txt
for i in $(cat pattern.txt)
do
echo $i
j=$(grep -c "$i" file1)
echo $j
if [ $j -ne 0 ]
then
echo $i >> matches.txt
fi
done
Basically you have to use
command substitution to put the result of grep into the j variable: the syntax uses backticks ` (not single quotes as in your code) or even better:
Then pay attention to the spaces inside the if/then expression: they are required to separate the square brackets from the expression. Check the differences with your version for other issues and feel free to ask if something is still not clear.