If the grep command returns any result set

my intension is to use a grep command inside the shell script
and if any row is returned or not..
depending on the resultset i have to code on further.

how to check this
i mean.. could anyone help me out with the if condition how to use it here !!

This is a faq, just use the search engine right above and search for if grep.

Gotam,

You can use the below code to test whether you got some rows using grep.....

 
count=`grep "search pattern" filename|wc -l`
if [ $count -ne 0 ]
then
     echo "pattern match found"
else
     echo "no match with the pattern"
fi

Hope this is what you are looking for....

Thanks

While this would certainly work, it's more complex than it has to be. What I believe the OP is (should be) looking for are exit statuses. You should do some googling to get some more info, but I'll give you a quick rundown.

When you run a command, after it completes there is something called an exit value. Generally if a command is successful, the exit value is 0, and if it fails, the value is 1 (if you look in the man pages for a command, sometimes it may have a whole list of exit statuses for each possible scenario). You can see what a commands exit status is by looking at the variable $?.

So what you want is to check if the exit status of your grep command is a 0 or a 1:

Let's take a look below:

(03:28:07\[D@DeCoBox15)
[~]$ cat animals
cat
dog
hippo

(03:30:50\[D@DeCoBox15)
[~]$ grep dog animals
dog

(03:30:57\[D@DeCoBox15)
[~]$ echo $?
0

(03:31:00\[D@DeCoBox15)
[~]$ grep mouse animals

(03:31:07\[D@DeCoBox15)
[~]$ echo $?
1

As you can see, when the grep succeeds, the exit status was 0, and when it failed (because there is not mouse in the animals file) it was 1. So now all you have to do is build a if this than or if that....yadda yadda.

But wait...there's more. If you want to do something like this in a one liner you can use conditional checking syntax: && (if the last command succeeded) and || (if the last command failed). So again, if you want to use this with grep:

(03:31:12\[D@DeCoBox15)
[~]$ grep cat animals >/dev/null && echo "Yep, it is there" || echo "Sorry, could not find it"
Yep, it is there

(03:35:00\[D@DeCoBox15)
[~]$ grep mouse animals >/dev/null && echo "Yep, it is there" || echo "Sorry, could not find it"
Sorry, could not find it

So in this example, we're just echoing stuff based on the results...but we could be doing anything.

Hope this helps...it's amazing how much I'll type when I can't sleep.

Or perhaps easier to understand and if grep has the -q (quiet) option instead of > /dev/null:

if grep -q "search pattern" filename
then
  echo "pattern match found"
else
  echo "no match with the pattern"
fi