我可以通过grep命令在/etc/passwd中查找,但那似乎很繁琐。'finger'未安装,我想避免使用它。由于这是一个程序,因此最好有一些命令可以让您直接访问用户信息。
我可以通过grep命令在/etc/passwd中查找,但那似乎很繁琐。'finger'未安装,我想避免使用它。由于这是一个程序,因此最好有一些命令可以让您直接访问用户信息。
$ user_name=foo
$ user_record="$(getent passwd $user_name)"
$ echo "$user_record"
foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash
由于历史原因,用户的全名被记录在一个称为“GECOS”字段的字段中;为了使事情更加复杂,这个字段通常有其自己的结构,其中完整姓名只是几个可选子字段之一。因此,任何想要从帐户记录中获取完整姓名的内容都需要解析这两个层次。
$ user_record="$(getent passwd $user_name)"
$ user_gecos_field="$(echo "$user_record" | cut -d ':' -f 5)"
$ user_full_name="$(echo "$user_gecos_field" | cut -d ',' -f 1)"
$ echo "$user_full_name"
Fred Nurk
你的编程语言很可能有一个库函数可以用更少的步骤来完成这个任务。在C语言中,你可以使用“getpwnam”函数,然后解析GECOS字段。
在现代glibc系统上,使用此命令:
getent passwd "username" | cut -d ':' -f 5
这将获取指定用户的passwd
条目,与底层NSS模块无关。
阅读getent
的手册页面。
如果您已经在编程,可以使用getpwnam()
C函数:
struct passwd *getpwnam(const char *name);
passwd
结构体有一个 pw_gecos
成员,应该包含用户的全名。getpwnam()
的手册页。,
) 作为分隔符,并将用户真实姓名放在第一位。getent passwd $USER | cut -d ':' -f 5 | cut -d ',' -f 1
。 - jpyams$USER
周围加上双引号,以防它包含特殊字符,并且在 :
和 ,
周围不加引号:getent passwd "$USER" | cut -d: -f5 | cut -d, -f1
。 - Alexandergetent passwd "${USER}" | cut -d: -f5
在非sudo环境下会返回 ,,,
。编辑:我认为是因为创建的用户没有名字。 - MaXi32结合其他答案,在最小的Debian/Ubuntu安装上进行测试:
getent passwd "$USER" | cut -d ':' -f 5 | cut -d ',' -f 1
whoami
命令,你可以直接使用任何用户名作为参数。如果一定要使用子shell,请不要使用反引号,而是使用 $()
。但在这种情况下,使用 "$USER"
更加合适。 - MestreLion如果您想用C语言实现此操作,请尝试以下方法:
#include <sys/types.h>
#include <pwd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
/* Get full name of a user, given their username. Return 0 for not found,
-1 for error, or 1 for success. Copy name to `fullname`, but only up
to max-1 chars (max includes trailing '\0'). Note that if the GECOS
field contains commas, only up to to (but not including) the first comma
is copied, since the commas are a convention to add more than just the
name into the field, e.g., room number, phone number, etc. */
static int getfullname(const char *username, char *fullname, size_t max)
{
struct passwd *p;
size_t n;
errno = 0;
p = getpwnam(username);
if (p == NULL && errno == 0)
return 0;
if (p == NULL)
return -1;
if (max == 0)
return 1;
n = strcspn(p->pw_gecos, ",");
if (n > max - 1)
n = max - 1;
memcpy(fullname, p->pw_gecos, n);
fullname[n] = '\0';
return 1;
}
int main(int argc, char **argv)
{
int i;
int ret;
char fullname[1024];
for (i = 1; i < argc; ++i) {
ret = getfullname(argv[i], fullname, sizeof fullname);
if (ret == -1)
printf("ERROR: %s: %s\n", argv[i], strerror(errno));
else if (ret == 0)
printf("UNKONWN: %s\n", argv[i]);
else
printf("%s: %s\n", argv[i], fullname);
}
return 0;
}
试试这个:
getent passwd eutl420 | awk -F':' '{gsub(",", "",$5); print $5}'
前两个答案可以合并成一行:
getent passwd <username> | cut -d ':' -f 5 | cut -d ',' -f 1
我的代码在bash和ksh中可用,但在dash或旧版Bourne shell中不可用。它也读取其他字段,以防您需要它们。
IFS=: read user x uid gid gecos hm sh < <( getent passwd $USER )
name=${gecos%%,*}
echo "$name"
while IFS=: read user x uid gid gecos hm sh; do
name=${gecos%%,*}
[ $uid -ge 1000 -a $uid -lt 60000 ] && echo "$name"
done < /etc/passwd
在Linux上将全名存入变量的方法是:
u_id=`id -u`
uname=`awk -F: -vid=$u_id '{if ($3 == id) print $5}' /etc/passwd`
然后只需简单地使用该变量,例如:$ echo $uname
第一步:
$ user_name=sshd
$ awk -F: "\$1 == \"$user_name\" { print \$5 }" /etc/passwd
Secure Shell Daemon
然而,passwd数据库支持在gecos中使用特殊字符'&',应该用用户名的大写值替换它:
$ user_name=operator
$ awk -F: "\$1 == \"$user_name\" { print \$5 }" /etc/passwd
System &
这里的大多数答案(除了指纹解决方案)都没有尊重 &。 如果您想支持此情况,则需要一个更复杂的脚本。
第二个方案:
$ user_name=operator
$ awk -F: "\$1 == \"$user_name\" { u=\$1; sub(/./, toupper(substr(u,1,1)), u);
gsub(/&/, u, \$5); print \$5 }" /etc/passwd
System Operator
老牌的finger命令也可能有所帮助 :-)
finger $USER |head -n1 |cut -d : -f3
getent passwd | grep "$USER" | cut -d":" -f5 | cut -d"," -f1
- Dean Rather