grep return 0 if no match

15,584

Solution 1

The actual answer to this question is to add || true to the end of the command, e.g.:

echo thing | grep x || true

This will still output a 0 return code.

Solution 2

You are processing the return value incorrectly.

value=$( grep -ic "210.64.203" /var/logs )

sets value to the output of the grep, not to its return code.

After executing a command the exit code it stored in $?, but you usually don't need it.

if grep -ic "210.64.203" /var/logs 
then echo "Found..."
else echo "not found"
fi

If you want the value, then test for content.

rec="$( grep -ic "210.64.203" /var/logs )"
if [ -n "$rec" ] ; then echo found; fi

Or if using bash,

if [[ "$rec" ]] ; then echo found; fi

though I prefer to be explicit -

if [[ -n "$rec" ]] ; then echo found; fi

Solution 3

[user@host ~]$ cat ~/mylogfile.txt
no secrets here
[user@host ~]$ grepnot(){ ! grep $1 $2; return $?;}
[user@host ~]$ grepnot password mylogfile.txt 
[user@host ~]$ echo $?
0
[user@host ~]$ grepnot secret mylogfile.txt 
no secrets here
[user@host ~]$ echo $?
1

Note: I'm using 'grepnot' in a Jenkins pipeline. My previous answer used "1 - $?" to reverse the return code. But that solution still caused grep failure in the pipeline. The above solution (which borrows from a previous "!" answer) works with the pipeline.

Share:
15,584
julian lee
Author by

julian lee

Updated on June 04, 2022

Comments

  • julian lee
    julian lee almost 2 years

    Is there any bash script which can return me a result=true with command grep?

    Example: There are 1000 records of 103.12.88 in my CF logs. Can I do a grep 103.12.88 if detect 1 or more results then print/output result show me either YES or True

  • William Pursell
    William Pursell over 4 years
    Rather than if [ ! "$rec" = "" ], it is generally cleaner to write if [ -n "$rec" ]
  • Paul Hodges
    Paul Hodges over 4 years
    Edited. I always use that with [[ in bash, but since I so seldom use [ it didn't occur to me to try it while I was testing. Thanks.
  • Paul Hodges
    Paul Hodges almost 4 years
    No it doesn't. That forces a 0 return, but generates no output at all. Try ret=$( echo thing | grep x || true ) vs ret=$( echo thing | grep -q x; echo $?; ) The former leaves $ret empty. The latter loads the exit code. Maybe you meant echo thing | grep x && echo true ?
  • sebcoe
    sebcoe almost 4 years
    Nope. I want to always return a 0 return code regardless of whether grep matches or not (which in the example it won't).
  • Paul Hodges
    Paul Hodges almost 4 years
    exit code and output are separate things. OP says Can I do a grep 103.12.88 if detect 1 or more results then print/output result show me either YES or True, which is output. Your solution is fine, it just doesn't do what the poorly worded OP asked.
  • lonix
    lonix about 3 years
    This doesn't work - there is no not in bash, and the second line does indeed print. Are you using a different shell?
  • Telmo Trooper
    Telmo Trooper about 3 years
    Now that you mentioned it, it seems not is a program that comes with LLVM (which I already had installed). I'll try to gather up more information to update my answer, but thank you for noticing the issue.
  • lonix
    lonix about 3 years
    Was wondering why it didn't work for me :)
  • ob-ivan
    ob-ivan almost 3 years
    A single exclamation mark ! does what the reponder's not program does: it 'inverts' the status code. So ! true exits with the status code 1 and ! false exits with the status code 0.