我可以通过/ etc / passwd进行grep,但这看起来很麻烦。 'finger'没有安装,我想避免这种依赖。这是一个程序,所以如果有一些命令可以让你只访问用户信息,那就太好了。

有帮助吗?

解决方案

你没有指定编程语言,所以我假设你想要使用shell;这是 Posix炮弹的答案

这两步:获取适当的记录,然后从该记录中获取所需的字段。

首先,通过查询 passwd 来获取帐户记录:

$ 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-Function:

struct passwd *getpwnam(const char *name);

passwd struct有一个 pw_gecos 成员,该成员应包含用户的全名。

阅读 getpwnam() 的联机帮助页。


请注意,许多系统使用此字段的次数超过用户的全名。最常见的约定是在字段中使用逗号()作为分隔符,并首先放置用户的真实姓名。

如果您想从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;
}

在最小的Debian / Ubuntu安装上测试的其他答案的组合:

getent passwd `whoami` | cut -d ':' -f 5 | cut -d ',' -f 1

试试这个:

getent passwd eutl420 | awk -F':' '{gsub(",", "",$5); print $5}'

前两个答案可以合并为一行:

getent passwd <username> | cut -d ':' -f 5 | cut -d ',' -f 1

我的代码在bash和ksh中运行,但不是破折号或旧的Bourne shell。它也会读取其他字段,以防您可能需要它们。

IFS=: read user x uid gid gecos hm sh < <( getent passwd $USER )
name=${gecos%%,*}
echo "$name"

您还可以扫描整个/ etc / passwd文件。这适用于普通的Bourne shell,在1个进程中,但与LDAP或什么不同。

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

另一方面,使用工具很好。 C也很好。

我在Linux上用它来获取变量的全名的方式是:

u_id=`id -u`
uname=`awk -F: -vid=$u_id '{if ($3 == id) print $5}' /etc/passwd`

然后只需简单地使用变量ex: $ echo $ uname

拿1:

$ user_name=sshd
$ awk -F: "\$1 == \"$user_name\" { print \$5 }" /etc/passwd
Secure Shell Daemon

但是,passwd数据库支持特殊字符'&amp;'在gecos中,应替换为用户名的大写值:

$ user_name=operator
$ awk -F: "\$1 == \"$user_name\" { print \$5 }" /etc/passwd
System &

这里的大部分答案(手指解决方案除外)都不尊重&amp ;. 如果你想支持这种情况,那么你需要一个更复杂的脚本。

拿2:

$ 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 $USER |head -n1 |cut -d : -f3
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top