I need to grep for an exact word in password file but I get two responses back

1.2k views Asked by At

I am using grep to parse the password file.

When I use

grep -w "avahi" /etc/passwd

I get two responses
avahi and avahi-autoipd
I have not found a method to give me the unique response.

This command is part of a bigger script where the name (avahi) is actually a variable.

This does work when the name is rpc and rpcuser. So I am guessing the it has something to do with the dash (-) in the name.

Actual code:

#!/bin/ksh
getent shadow |cut -d: -f1-2|grep ':!!'| cut -d: -f1 > /tmp/pasck
while read line
do 
   NOLOGIN=`grep -w $line /etc/passwd | cut -d -f7|cut -d/ -f3`
if [[ $NOLOGIN != "nologin: && $NOLOGIN != "false" ]] ; then
echo "$line" "$NOLOGIN" >> /tmp/pasck.list
fi
done <?tmp/pasck

The script is trying to go through the shadow file and look for users with no passwords. Then I compare the results to the passwd file to find which of those accounts are set to /bin/false or /sbin/nologin. The remainder would be actual users with no password set but allowed on the system.

2

There are 2 answers

1
Mureinik On

Keeping things simple - you could include colon that comes after the username in your grep statement:

$ grep "^avahi:" /etc/passwd
0
Henk Langeveld On

You can use and explicitly test the first colon-separated field:

awk -F: '$1 = "avahi"' /etc/passwd