Grep no results

Hello guys,

I have been looking around but can't find the answer to my problem:
If the grep command displays no results, print "no results have been found" and increment x. But if the grep command find something, do nothing.

if [ grep -f file1 file2 ==  ]
   echo "no results have been found $x"
   x=`expr $x + 1 `
else
 do nothing

Many thanks for helping me.
Ben

Hi,

Try this one,

#! /usr/bin/bash
dir=`pwd`;
file=`echo -ne "$dir/file"`;
file1=`echo -ne "$dir/file1"`;
res=`fgrep -f $file -c $file1`;
if [ $res == 0 ];
then
echo "Pattern Not found";
else
echo "Pattern Found";
fi

Cheers,
Ranga:)

1 Like
if grep -f file1 file2
  echo "no results have been found $x"
  x=$(( $x + 1 ))
else
  do nothing
fi

Thanks to both of you for the quick reply.
Rangarasan I am taking your idea, that was what I was looking for Many thanks

res=`fgrep -f $file -c $file1`;
if [ $res == 0 ];
 then
....