Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Whether a User Exists

Tags:

bash

I want to create a script to check whether a user exists. I am using the logic below:

# getent passwd test > /dev/null 2&>1 # echo $? 0 # getent passwd test1 > /dev/null 2&>1 # echo $? 2 

So if the user exists, then we have success, else the user does not exist. I have put above command in the bash script as below:

#!/bin/bash  getent passwd $1 > /dev/null 2&>1  if [ $? -eq 0 ]; then     echo "yes the user exists" else     echo "No, the user does not exist" fi 

Now, my script always says that the user exists no matter what:

# sh passwd.sh test yes the user exists # sh passwd.sh test1 yes the user exists # sh passwd.sh test2 yes the user exists 

Why does the above condition always evaluate to be TRUE and say that the user exists?

Where am I going wrong?

UPDATE:

After reading all the responses, I found the problem in my script. The problem was the way I was redirecting getent output. So I removed all the redirection stuff and made the getent line look like this:

getent passwd $user  > /dev/null 

Now my script is working fine.

like image 588
slayedbylucifer Avatar asked Feb 11 '13 10:02

slayedbylucifer


People also ask

How do I check if a user exists?

user infomation is stored in /etc/passwd, so you can use "grep 'usename' /etc/passwd" to check if the username exist. meanwhile you can use "id" shell command, it will print the user id and group id, if the user does not exist, it will print "no such user" message. IT is stored in /etc/passwd IF nsswitch.

How do you find if a user exist in Linux?

Method #2: Find out if user exists in /etc/passwd file A quick shell script code: #!/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 # ....

How do I check if a group exists?

List All Members of a Group. To list all members of a group, use the getent group command followed by the group name. If there is no output that means the group doesn't exist.

What does &> mean in bash?

&>word (and >&word redirects both stdout and stderr to the result of the expansion of word. In the cases above that is the file 1 . 2>&1 redirects stderr (fd 2) to the current value of stdout (fd 1).


2 Answers

You can also check user by id command.

id -u name gives you the id of that user. if the user doesn't exist, you got command return value ($?)1

And as other answers pointed out: if all you want is just to check if the user exists, use if with id directly, as if already checks for the exit code. There's no need to fiddle with strings, [, $? or $():

if id "$1" &>/dev/null; then     echo 'user found' else     echo 'user not found' fi 

(no need to use -u as you're discarding the output anyway)

Also, if you turn this snippet into a function or script, I suggest you also set your exit code appropriately:

#!/bin/bash user_exists(){ id "$1" &>/dev/null; } # silent, it just sets the exit code if user_exists "$1"; code=$?; then  # use the function, save the code     echo 'user found' else     echo 'user not found' >&2  # error messages should go to stderr fi exit $code  # set the exit code, ultimately the same set by `id` 
like image 196
Kent Avatar answered Oct 14 '22 08:10

Kent


There's no need to check the exit code explicitly. Try

if getent passwd $1 > /dev/null 2>&1; then     echo "yes the user exists" else     echo "No, the user does not exist" fi 

If that doesn't work, there is something wrong with your getent, or you have more users defined than you think.

like image 24
chepner Avatar answered Oct 14 '22 06:10

chepner