71

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Duncan
  • 719
  • 1
  • 6
  • 7

8 Answers8

114

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

Marcellus
  • 1,305
  • 2
  • 9
  • 5
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
helloV
  • 50,176
  • 7
  • 137
  • 145
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
repzero
  • 8,254
  • 2
  • 18
  • 40
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) ]
alienchow
  • 383
  • 2
  • 7
  • 2
    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. – alienchow Dec 10 '14 at 03:03
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
antigenius
  • 121
  • 1
  • 3
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.

Ken
  • 11
  • 1
0

Shorter:

until ! grep $address /etc/passwd ; do {
    do_your_stuff
}
jehon
  • 1,404
  • 15
  • 21
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
NJT145
  • 3
  • 2