How can I check if 'grep' doesn't have any output?
Asked Answered
W

8

79

I need to check if the recipient username is in file /etc/passwd which contains all the users in my class, but I have tried a few different combinations of if statements and grep without success. The best I could come up with is below, but I don't think it's working properly.

My logic behind it is that if the grep is null, the user is invalid.

send_email()
{
  message=
  address=
  attachment=
  validuser=1
  until [ "$validuser" = "0" ]
    do
    echo "Enter the email address: "
    read address
    if [ -z grep $address /etc/passwd ]
      then
    validuser=0
    else
        validuser=1
    fi
    echo -n "Enter the subject of the message: "
    read message
    echo ""
    echo "Enter the file you want to attach: "
    read attachment
    mail -s "$message" "$address"<"$attachment"
    done
    press_enter
}
Wet answered 10/12, 2014 at 2:31 Comment(0)
B
120

Just do a simple if like this:

if grep -q $address  /etc/passwd
then 
   echo "OK";
else
   echo "NOT OK";
fi

The -q option is used here just to make grep quiet (don't output...)

Boatman answered 10/12, 2014 at 2:40 Comment(5)
simplier solution if you don't want to keep resultFlagitious
How can this be combined with a test of variable value greater than 0? something like if grep -q $address /etc/passwd && $val > 0Fault
@SumitKumarGhosh -- if grep -q "$address" /etc/passwd && [ "${val}" -gt 0 ]; thenWatersick
grep -q not working in solaris.. any idea or alternative?Essam
I found my answer here - #2481084Parkins
A
23

Use getent and check for grep's exit code. Avoid using /etc/passwd. Equivalent in the shell:

getent passwd | grep -q valid_user
echo $?

Output:

0

And:

getent passwd | grep -q invalid_user
echo $?

Output:

1
Araxes answered 10/12, 2014 at 2:40 Comment(1)
Why should /etc/passwd be avoided?Butane
C
19

Your piece of code:

if [ -z grep $address /etc/passwd ]

You haven't saved the results of grep $address /etc/passwd in a variable. Before putting it in the if statement and then testing the variable to see if it is empty.

You can try it like this:

    check_address=`grep $address /etc/passwd`
    if [ -z "$check_address" ]
      then
    validuser=0
    else
        validuser=1
    fi
Chromatophore answered 10/12, 2014 at 4:3 Comment(0)
P
12

The -z check is for variable strings, which your grep isn't giving. To give a value from your grep command, enclose it in $():

if [ -z $(grep $address /etc/passwd) ]
Paediatrician answered 10/12, 2014 at 2:41 Comment(1)
Could you try adding double quotes? if [ -z "$(grep $address /etc/passwd)" ] Edit: Also, shift your entire chunk of echos and mailing commands into the else clause if you do not want them to be run for an invalid user.Paediatrician
K
9

The easiest one will be this:

cat test123

# Output: 12345678

cat test123 | grep 123 >/dev/null && echo "grep result exist" || echo "grep result doesn't exist"

# Output: grep result exist

cat test123 | grep 999 >/dev/null && echo "grep result exist" || echo "grep result doesn't exist"

# Output: grep result doesn't exist
Keel answered 31/8, 2019 at 11:26 Comment(0)
A
1

My problem was that the file I was trying to grep was a binary file. On windows, the first two characters in the file were little squares. On mac, the first two characters were question marks. When I used more or less on the file, I could see it was binary and when I used diff, it responded that the "Binary files foo.log and requirements.txt differ".

I used cat to display the contents of the file, highlighted and copied the text (minus the two question marks at the top, deleted the file, created a new file with touch and then used vi to paste the text back into the new file.

After that, grep worked fine.

Annulus answered 23/3, 2022 at 14:46 Comment(0)
L
0

Shorter:

until ! grep $address /etc/passwd ; do {
    do_your_stuff
}
Leonardaleonardi answered 20/1, 2023 at 13:43 Comment(0)
F
0

As alienchow said, You can use parantheses with $(). But in some cases, you need to use it in quotes. For example, in order to check if my kvm is intel or amd, I use this bash code:

if [ ! -z "$(lsmod | grep kvm_intel)" ]; then
    echo "kvm_intel found."
elif [ ! -z "$(lsmod | grep kvm_amd)" ]; then
    echo "kvm_amd found."
else
    echo "check your kvm with command 'lsmod | grep kvm' ."
fi
Fraenum answered 22/8, 2023 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.