# If the grep command returns any result set

**URL:** <https://community.unix.com/t/if-the-grep-command-returns-any-result-set/264552>\
**Category:** Shell Programming and Scripting\
**Created:** [April 24, 2010, 8:44am UTC](https://community.unix.com/t/if-the-grep-command-returns-any-result-set/264552 "2010-04-24T08:44:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gotam](https://community.unix.com/letter_avatar/gotam/32/6_22734026db12803ebb3e059622ac3534.png) [@gotam](https://community.unix.com/u/gotam)\
**Post date:** [April 24, 2010, 8:44am UTC](https://community.unix.com/t/if-the-grep-command-returns-any-result-set/264552/1 "2010-04-24T08:44:00Z")

</div>

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 !!

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [April 24, 2010, 8:51am UTC](https://community.unix.com/t/if-the-grep-command-returns-any-result-set/264552/2 "2010-04-24T08:51:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spider007](https://community.unix.com/letter_avatar/spider007/32/6_22734026db12803ebb3e059622ac3534.png) [@spider007](https://community.unix.com/u/spider007)\
**Post date:** [April 24, 2010, 3:43pm UTC](https://community.unix.com/t/if-the-grep-command-returns-any-result-set/264552/3 "2010-04-24T15:43:33Z")

</div>

Gotam,

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

```nohighlight
 
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

---

<div class="post-metadata">

**Author:** ![DeCoTwc](https://community.unix.com/letter_avatar/decotwc/32/6_22734026db12803ebb3e059622ac3534.png) [@DeCoTwc](https://community.unix.com/u/DeCoTwc)\
**Post date:** [April 25, 2010, 3:39am UTC](https://community.unix.com/t/if-the-grep-command-returns-any-result-set/264552/4 "2010-04-25T03:39:00Z")

</div>

> [@spider007](#):
>
> Gotam,
> 
> You can use the below code to test whether you got some rows using grep.....
> 
> ```plaintext
>  
> 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:

```plaintext
(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:

```plaintext
(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.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 25, 2010, 4:12am UTC](https://community.unix.com/t/if-the-grep-command-returns-any-result-set/264552/5 "2010-04-25T04:12:34Z")

</div>

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

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

```
