How can I find out, in a shell script, whether a given user name exists on the current system?
/etc/passwd
and /etc/shadow
are incomplete. Consider OS X's Directory Services, or Linux with Likewise Active Directory integration.
Answer
One of the most basic tools to be used for that is probably id
.
#!/bin/bash
if id "$1" >/dev/null 2>&1; then
echo "user exists"
else
echo "user does not exist"
fi
Which produces
$ ./userexists root
user exists
$ ./userexists alice
user does not exist
$ ./userexists
user does not exist
Comments
Post a Comment