bash 检查用户是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14810684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:19:24  来源:igfitidea点击:

Check Whether a User Exists

bash

提问by slayedbylucifer

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:

所以如果用户存在,那么我们就成功了,否则用户不存在。我已将上述命令放在 bash 脚本中,如下所示:

#!/bin/bash

getent passwd  > /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?

为什么上述条件总是评估为 TRUE 并说用户存在?

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 getentoutput. So I removed all the redirection stuff and made the getentline look like this:

阅读所有回复后,我在我的脚本中发现了问题。问题是我重定向getent输出的方式。所以我删除了所有重定向的东西,让这getent行看起来像这样:

getent passwd $user  > /dev/null

Now my script is working fine.

现在我的脚本工作正常。

回答by Kent

you can also check user by idcommand.

您也可以通过id命令检查用户。

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

id -u name给你那个用户的id。如果用户不存在,您将获得命令返回值 ( $?)1

回答by poitroae

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.

如果用户存在,它将返回 1(因为用户最多有 1 个条目),如果用户不存在则返回 0。

回答by chepner

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

无需明确检查退出代码。尝试

if getent passwd  > /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.

如果这不起作用,则getent您的 有问题,或者您定义的用户比您想象的要多。

回答by Daniel Sokolowski

This is what I ended up doing in a Freeswitchbash startup script:

这就是我最终在Freeswitchbash 启动脚本中所做的:

# 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

回答by Valdas Rap?evi?ius

I suggest to use id command as it tests validuser existence wrt passwd file entry which is not necessary means the same:

我建议使用 id 命令,因为它测试有效的用户存在 wrt passwd 文件条目,这不是必需的,意思相同:

if [ `id -u $USER_TO_CHECK 2>/dev/null || echo -1` -ge 0 ]; then 
echo FOUND
fi

Note: 0 is root uid.

注意:0 是根 uid。

回答by Takeda

I was using it in that way:

我是这样使用它的:

if [ $(getent passwd $user) ] ; then
        echo user $user exists
else
        echo user $user doesn\'t exists
fi

回答by Fleshgrinder

By far the simplest solution:

到目前为止最简单的解决方案:

if id -u user >/dev/null 2>&1; then
    echo user exists
else
    echo user missing
fi

The >/dev/null 2>&1can be shortened to &>/dev/nullin Bash.

>/dev/null 2>&1可缩短到&>/dev/nullBash中。

回答by Bakul Gupta

Script to Check whether Linux user exists or not

检查Linux用户是否存在的脚本

Script To check whether the user exists or not

Script 检查用户是否存在

#! /bin/bash
USER_NAME=bakul
cat /etc/passwd | grep ${USER_NAME} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
    echo "User Exists"
else
    echo "User Not Found"
fi

回答by Hymanotonye

Late answer but fingeralso shows more information on user

迟到的答案,但finger也显示了有关用户的更多信息

  sudo apt-get finger 
  finger "$username"

回答by Yu Chai

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.

用户信息存储在 /etc/passwd 中,因此您可以使用“grep 'usename' /etc/passwd”来检查用户名是否存在。同时你可以使用“id”shell命令,它会打印用户ID和组ID,如果用户不存在,它会打印“没有这样的用户”消息。