bash 如何在 UNIX 上获得用户友好的用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13241964/
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-18 03:42:38  来源:igfitidea点击:

How do I get a user's friendly username on UNIX?

bashunix

提问by James A. Rosen

I want to get the "friendly" name, not the username, at least if such a string exists for the given user. Things I've tried:

我想获得“友好”名称,而不是用户名,至少如果给定用户存在这样的字符串。我尝试过的事情:

whoami
jamesarosen

id -un
jamesarosen

id -p
uid jamesarosen
groups  staff com.apple.access_screensharing ...

id -P
jamesarosen:********:501:20::0:0:James A. Rosen:/Users/jamesarosen:/bin/bash

That last one has the information I'm looking for, but I'd prefer not to have to parse it out, particularly since I'm not terribly confident that the format (specifically the number of :s) will remain consistent across OSes.

最后一个包含我正在寻找的信息,但我不想解析它,特别是因为我不太相信格式(特别是:s的数量)将在不同操作系统中保持一致。

回答by Todd A. Jacobs

Parse the GECOS Field for User's Full Name

解析用户全名的 GECOS 字段

The format of /etc/passwd and most of the GECOS fieldis extremely well standardized across Unix-like systems. If you find an exception, by all means let us know. Meanwhile, the easiest way to get what you want on a Unix-like system is to use getentand cutto parse the GECOS field. For example:

/etc/passwd 的格式和大部分GECOS 字段在类 Unix 系统中都非常标准化。如果您发现异常,请务必告知我们。同时,在类 Unix 系统上获得所需内容的最简单方法是使用getentcut解析 GECOS 字段。例如:

getent passwd $LOGNAME | cut -d: -f5 | cut -d, -f1

回答by Cez

The only way that I know would be to parse it:

我知道的唯一方法是解析它:

grep -P "^$(whoami):" /etc/passwd | cut -f5 -d:

You can be pretty certain of the format of /etc/passwd

您可以非常确定 /etc/passwd 的格式

回答by Ansgar Wiechers

You could use fingerto obtain that information:

您可以使用以下finger方法获取该信息:

finger `id -un` | head -1 | cut -d: -f3-

which has the advantage (or disadvantage, depending on your requirements) that it will retrieve the information for non-local users as well.

这具有优势(或劣势,取决于您的要求),它也将为非本地用户检索信息。

If you only want to get the information from /etc/passwd, you'll most likely have to parse the file one way or the other, as others have already mentioned. Personally I'd prefer awkfor this task:

如果您只想从 获取信息/etc/passwd,您很可能必须以一种或另一种方式解析文件,正如其他人已经提到的那样。我个人更喜欢awk这个任务:

awk -F: -vid=`id -u` '{if ( == id) print }' /etc/passwd

回答by David W.

Take a look at the /etc/passwdfile. This file shows you how user information is stored. Your user information may or may not be stored here (There are several different databases that Unix uses for storing users), but the format is the same.

看一下/etc/passwd文件。该文件向您展示了用户信息的存储方式。您的用户信息可能存储也可能不存储在这里(Unix 用于存储用户的数据库有多种不同),但格式是相同的。

Basically, Unix uses the User ID (UID) to store what user is what. The next entry was the old password entry, then the UID, the primary Group ID, the GECOSfield, the $HOMEdirectory, and the user's shell. (There are three extra entries displayed in the id -Pcommand in MacOS. I don't know what they are, but they make the GECOS field the eighth field instead of the fifth field).

基本上,Unix 使用用户 ID (UID) 来存储用户是什么。下一个条目是旧密码条目,然后是 UID、主要组 ID、GECOS字段、$HOME目录和用户外壳。(id -PMacOS中的命令中显示了三个额外的条目。我不知道它们是什么,但它们使 GECOS 字段成为第八个字段而不是第五个字段)。

Using the id -Pcommand on your system gave you this entry. Some systems use getentor even getpwentas a command. What you need to do is parse this entry. Each field is separated by colons, so you need either the fifth or eighth the entry (depending upon the command you had to use).

id -P在您的系统上使用该命令会为您提供此条目。一些系统使用getent甚至getpwent作为命令。您需要做的是解析此条目。每个字段由冒号分隔,因此您需要第五个或第八个条目(取决于您必须使用的命令)。

The awkand cutcommands do this quite nicely. cutis probably more efficient, but awkis more common, so I tend to use that.

awkcut命令做到这一点相当不错。cut可能更有效,但awk更常见,所以我倾向于使用它。

In awk, the standard field separator is white space, but you can use the -Fparameter to change this. In Awk, each field in a line is given a number and preceded by a dollar sign. The $0field is the entire line.

在 中awk,标准字段分隔符是white space,但您可以使用-F参数来更改它。在 Awk 中,一行中的每个字段都被赋予一个数字并以美元符号开头。该$0字段是整行。

Using awk, you get:

使用awk,您将获得:

id -P | awk -F: '{print }'

This says to take the id -Pcommand, and use the :as a field separator, and to print out the eighth field. THe curly braces surround all AWK programs, and the single quotes are needed to keep the shell from interpreting the $8.

这表示接受id -P命令,并将:用作字段分隔符,并打印出第八个字段。花括号将所有AWK 程序括起来,并且需要单引号以防止 shell 解释$8.

In BASH, you can use $( )to run a command and return it's output, so you can set environment variables:

在 BASH 中,您可以使用$( )来运行命令并返回它的输出,因此您可以设置环境变量:

$USER_NAME=$(id -P | awk -F: `{print }`)
echo $USER_NAME

回答by Matthew Schinckel

On macOS at least (and probably other *BSD-alikes), you may use: id -Fto get just the full name.

至少在 macOS 上(可能还有其他类似 *BSD 的系统),您可以使用:id -F来获取全名。