Using Grep in an If Statement

grep in IF statement

The argument you pass in to grep, $next, is being treated as a list of filenames to search through. If you would like to search within that line for a string, say, MO, then you need to either put it in a file and pass that file in as an argument, or pipe it in via standard input.

Here's an example that you can try out on the command line; of course, substitute the variable that you're using for the literal value that I included to illustrate:

if echo "40922|OPR MO 12345|OPR MO 12345|12345|202|local|LMNO" | grep -q "MO"
then echo "FOUND"
else echo "NOT FOUND"
fi

How to use grep in if statement in shell

There is very likely no command named [grep. Drop the [

if grep -q "<pr>$i</pr>" ./archiv; then ...

[ is not and has never been a part of the shell grammar. It is a command, just like echo or test or grep. The value returned by that command is used to determine whether or not to execute the clause of the if statement.

Using grep in an if statement

Just remove the square brackets and pass $i to grep:

if echo $i | grep -q "|0510"

In your code sample, grep does not have anything to work on.

grep command in an if statement

Here's another way to cache the results: mapfile consumes its stdin into an array, each line is an array element.

mapfile -t results < <(grep -wi "$searchT" test.txt)
num=${#results[@]}

if ((num == 0)); then
echo "no match found"
else
echo "found $num matches"
printf "%s\n" "${results[@]}"
fi

How to use grep in an if statement?

Use grep -q option:

if grep -q --include='*.java' -Ri 'System.loadLibrary' .; then
echo "found a matching file"
fi

how can i use grep result from a variable in if/else statment

You're mixing 2 types of results here : result (i.e. displayed text) and return value.

TL;DR

You can verify if the variable $chkX is not empty with [[ ! -z ${chkX} ]], such as :

if [[ ! -z ${chk1} ]] && [[ ! -z ${chk2} ]] && [[ ! -z ${chk3} ]]; then
[...]

Or you can do something like this, based on exit codes.

yum list installed | grep -q vim
if [[ $? -eq 0 ]]; then
# do your stuff here when package is installed
else
#.. do your stuff here when package isn't installed ..
fi

or

if yum list installed | grep -q 'vim' ;then
# do your stuff here when package is installed
else
# .. do your stuff here when package isn't installed ..
fi


When executing in a subshell through $(yum ...) you are storing the result (i.e. displayed text) that's echoed by the command.

For instance:

$> yum list installed | grep vim
vim-common.x86_64 2:8.2.3755-1.fc35 @updates

If you want the return code or exit code, it's accessible through $?.

For example:

$> ls
[...]
$> echo $?
0
$> ls toto
ls: cannot access 'toto': No such file or directory
$> echo $?
2

Note every command changes the exit code.

So accessing twice $? will change its value.

$> ls toto
ls: cannot access 'toto': No such file or directory
$> echo $?
2
$> echo $?
0

In your case, you're testing if the text given by yum is mathematically equal to 0:

chk3=$(yum list installed | grep rpmdev)
if [[ $chk1 -ne 0 && "$chk2" -ne 0 && "$chk3" -ne 0 ]];then
^^^^^^

It's not possible because :

  • no package found -> empty variable
  • one or many package found -> single or multiline text with package information.
  • and you'll never get 0.

If you run it with test you have various cases of failure:

# note: rpmdevtools is not installed on by computer
$> chk1=$(yum list installed | grep rpmdevtools)
$> test $chk1 -ne 0
bash: test: -ne: unary operator expected

# multiple vim-* packages are installed
$> chk1=$(yum list installed | grep vim)
$> test $chk1 -ne 0
bash: test: too many arguments

Use grep inside a if statement

Backtick substitution will return the output of the command. In this case, you are outputting the line number with grep's output, following by the matching line. Hence, there appears to be something on line 22, but the shell tries to execute the command 22:, which then fails.

The variant you have posted as a followup answer will work, as grep returns 0 on a successful match, but 1 if the match fails.



Related Topics



Leave a reply



Submit