UNIX - Linux Command To Check Existing Groups and Users
UNIX - Linux Command To Check Existing Groups and Users
A. You can easily check the existing users and groups under Linux using the following
commands.
vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash
#!/bin/bash
# init
USERID="$1"
#....
/bin/egrep -i "^${USERID}" /etc/passwd
if [ $? -eq 0 ]; then
echo "User $USERID exists in /etc/passwd"
else
echo "User $USERID does not exists in /etc/passwd"
fi
# ....
Normally, exit status is 0 returned if user accounts (lines) are found and 1 otherwise.
id command
id is another command to display user / group information for any USERNAME, or the current
user. To find out more about user called, tom, enter:
$ id tom
Sample output
id command exit status is 0 returned if user accounts (lines) are found and 1 otherwise. A sample
shell script using id command:
#!/bin/bash
USERID="$1"
/bin/id $USERID 2>/dev/null
[ $? -eq 0 ] && echo "User found" || echo "User not found"
/bin/id -g $USERID 2>/dev/null
[ $? -eq 0 ] && echo "Group found" || echo "Group not found"