julian lee
julian lee

Reputation: 551

grep return 0 if no match

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

Upvotes: 42

Views: 70468

Answers (6)

mikelin
mikelin

Reputation: 456

Actually I'd make it

echo thing | ( grep x || [ "$?" == "1" ] ) | <next command>

to catch any errors which may occur (disk, memory, etc.). This is especially useful if you use set -e on your script.

https://www.gnu.org/software/grep/manual/html_node/Exit-Status.html

Upvotes: 2

vp124
vp124

Reputation: 435

The title of the question is misleading, but since it hasn't been changed, I think it's important to have the answer to that particular quesion for those who are looking it. And the answer is simple: grep -v does it: it inverts the exit code.

$ echo thing | grep x -vqz
$ echo $?
0
$ echo thing | grep thing -vqz
$ echo $?
1

Note that you need -z to stream the entire file as one line; otherwise, in multi-line files you'll get exit 0 whether the match is found or not. (-q flag is just to suppress the output.)

Credit: https://unix.stackexchange.com/questions/433711/grep-exit-with-1-if-match

Upvotes: 13

WayneTabor
WayneTabor

Reputation: 11

[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.

Upvotes: 1

Telmo Trooper
Telmo Trooper

Reputation: 5694

If you want return success on grep not finding a match it's easier to negate its output:

echo "Hello, World" | not grep -q "Friend" && echo "No match"

This prints No match since "Friend" was not found in the output.

echo "Hello, World" | not grep -q "Hello" && echo "No match"

This prints nothing since friend was found in the output.

Note we're using flag -q (as in --quiet) so grep does not write to output, but exits with status 0 if a match is found.

Upvotes: -1

sebcoe
sebcoe

Reputation: 812

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.

Upvotes: 65

Paul Hodges
Paul Hodges

Reputation: 15293

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

Upvotes: 2

Related Questions