可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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
回答2:
Why don't you simply use
grep -c '^username:' /etc/passwd
It will return 1 (since a user has max. 1 entry) if the user exists and 0 if it doesn't.
回答3:
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.
回答4:
This what I ended up doing in a Freeswitch
bash startup script:
# Check if user exists if ! id -u $FS_USER > /dev/null 2>&1; then echo "The user does not exist; execute below commands to crate and try again:" echo " root@sh1:~# adduser --home /usr/local/freeswitch/ --shell /bin/false --no-create-home --ingroup daemon --disabled-password --disabled-login $FS_USER" echo " ..." echo " root@sh1:~# chown freeswitch:daemon /usr/local/freeswitch/ -R" exit 1 fi
回答5:
I suggest to use id command as it tests valid user existence wrt passwd file entry which is not necessary means the same:
if [ `id -u $USER_TO_CHECK 2>/dev/null || echo -1` -ge 0 ]; then echo FOUND fi
Note: 0 is root uid.
回答6:
Actually I cannot reproduce the problem. The script as written in the question works fine, except for the case where $1 is empty.
However, there is a problem in the script related to redirection of stderr
. Although the two forms &>
and >&
exist, in your case you want to use >&
. You already redirected stdout
, that's why the form &>
does not work. You can easily verify it this way:
getent /etc/passwd username >/dev/null 2&>1 ls
You will see a file named 1
in the current directory. You want to use 2>&1
instead, or use this:
getent /etc/passwd username &>/dev/null
This also redirects stdout
and stderr
to /dev/null
.
Warning Redirecting stderr
to /dev/null
might not be such a good idea. When things go wrong, you will have no clue why.
回答7:
I was using it in that way:
if [ $(getent passwd $user) ] ; then echo user $user exists else echo user $user doesn\'t exists fi
回答8:
Login to the server. grep "username" /etc/passwd This will display the user details if present.
回答9:
Late answer but finger
also shows more information on user
sudo apt-get finger finger "$username"
回答10:
Depending on your shell implementation (e.g. Busybox vs. grown-up) the [
operator might start a process, changing $?
.
Try
getent passwd $1 > /dev/null 2&>1 RES=$? if [ $RES -eq 0 ]; then echo "yes the user exists" else echo "No, the user does not exist" fi