Check if user exists

2

How can I in bash check if a user exists, but in a way that script works on multiple systems?

A limited example:

grep <username> /etc/passwd

Does not work on systems that use NIS or LDAP to manage users.

    
asked by anonymous 25.03.2015 / 14:20

1 answer

2

The getent command should work in this case.

getent passwd <usuario>

The getent command displays database entries supported by Service Switch libraries , which are configured in the /etc/nsswitch.conf file.

Another alternative that might work in this case is to verify that the user's ID is valid through the id ", using with -u option:

if id -u "usuario" > /dev/null 2>&1; then
        echo "Utilizador existe"
else
        echo "O utilizador especificado não existe"
fi
    
25.03.2015 / 15:41