Can grep return true/false or are there alternative methods

As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message; if it doesn’t, append the arguments to the file. I understand how to write the if statement, but not how to use grep within a script. I understand that grep will look something like this

grep ^$1 schemas.txt

I feel like this should be much easier than I am making it.

I’m getting an error “too many arguments” on the if statement. I got rid of the space between grep -q and then got an error binary operator expected.

if [ grep -q ^$1 schemas.txt ]
then
        echo "Schema already exists. Please try again"
        exit 1
else
        echo "$@" >> schemas.txt
fi
Asked By: Lauren

||

grep returns a different exit code if it found something (zero) vs. if it hasn’t found anything (non-zero). In an if statement, a zero exit code is mapped to “true” and a non-zero exit code is mapped to false. In addition, grep has a -q argument to not output the matched text (but only return the exit status code)

So, you can use grep like this:

if grep -q PATTERN file.txt; then
    echo found
else
    echo not found
fi

As a quick note, when you do something like if [ -z "$var" ]…, it turns out that [ is actually a command you’re running, just like grep. On my system, it’s /usr/bin/[. (Well, technically, your shell probably has it built-in, but that’s an optimization. It behaves as if it were a command). It works the same way, [ returns a zero exit code for true, a non-zero exit code for false. (test is the same thing as [, except for the closing ])

Answered By: derobert

Another simple way is to use grep -c.

That outputs (not return as exit code), the number of lines that match the pattern, so 0 if there’s no match or 1 or more if there’s a match.

So, if you wanted to check that the pattern is matched 3 or more times, you would do:

if [ "$(grep -c "^$1" schemas.txt)" -ge 3 ]; then
  ...
Answered By: amigal

If you want to use it with square brackets, you can execute the below

if [ `grep -q PATTERN file.txt` ]; then
    echo found
else
    echo not found

This Logic works for all commands, Just place your commands inside backtick (the button above tab or to the down of Esc button or to the left of 1 button)

Answered By: k_vishwanath

If we want to catch the first word of a file we need do add -zw to grep

if grep -qzw "^$1" file
then 
   ... 
else 
   ... 
fi

Without -z we get the first word of a line. Without -w we get partial words.

Answered By: JJoao

I know I am late for this, but I love this short version:

grep -q ^$1 schemas.txt && echo "Schema already exists. Please try again" || echo "$@" >> schemas.txt
Answered By: Denis Pitzalis

Old post but really the answer was never given.
Yes Grep returns 0 if pattern is found (true) and 1 if the pattern is not found (false).
Keep in mind grep is line based so the search term you thought would do it all might give you a lot of garbage hits, I leave this up to you to figure out how to parse.
$? is the exit status, I prefer to use pipes rather than reading from a file as it suits my scripts better.
I use the following:

{cat/cut/echo/curl/whatever} | grep -q PATTERN |echo $?

If the above returns 0 your pattern was found, if it returns 1 it was not.

This is useful as I place it in while loops to monitor my processes and keep certain scripts alive if they die or notify me if they have completed.

Answered By: unifex

This is nice:

 varx='1680790311';if [[ deleteIfExists=$(grep -n "$varx" userdb.tmp | cut -d':' -f1) > 0 ]];then echo $deleteIfExists;fi
Answered By: clarke
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.