linux用户是否存在
How to test whether a user account, say linuxuser, already exist on Linux?
如何测试Linux上是否已经存在用户帐户(例如linuxuser)?
You may make use of id which tries to get user IDs.
您可以利用ID尝试获取用户ID。
The Bash code snippet is as follows.
Bash代码片段如下。
user=hello
id -u $user >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo "user $user already exist."
else
echo "user $user doesn't exist."
fi
Or a shorter one
或更短的一个
userexist=true
[ "`id -u $user 2>/dev/null`" == "" ] && userexist=false
The $userexist
variable will contains whether $user
already exists.
$userexist
变量将包含$user
是否已经存在。
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-test-whether-a-user-already-exist-on-linux/
linux用户是否存在