I could grep through /etc/passwd but that seems onerous. 'finger' isn't installed and I'd like to avoid that dependency. This is for a program so it would be nice if there was some command that let you just access user info.
相关问题
- How to get the return code of a shell script in lu
- Is shmid returned by shmget() unique across proces
- NOT DISTINCT query in mySQL
- how to get running process information in java?
- Flush single app django 1.9
Try this:
You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for Posix shells.
Two steps to this: get the appropriate record, then get the field you want from that record.
First, getting the account record is done by querying the
passwd
table:For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.
Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.
The way that I figured it on Linux to get the full name into a variable was:
Then just simple use the variable, ex:
$ echo $uname
On a modern glibc system, use this command:
That'll get you the
passwd
entry of the specified user, independent of the underlying NSS module.Read the manpage of
getent
.If you're already programming, you can use the
getpwnam()
C-Function:The
passwd
struct has apw_gecos
member which should contain the full name of the user.Read the manpage of
getpwnam()
.Be aware that many systems use this field for more than the full name of the user. The most common convention is to use a comma (
,
) as separator within the field and place the users real name first.The top two answers can be combined in one line:
Just in case you want to do this from C, try something like this: