6

in the file /etc/passwd we have the so called GECOS fields (which stands for "General Electric Comprehensive Operating System"), that is:

username:password:userid:groupid:gecos:home-dir:shell 

Where GECOS are divided as:

:FullName,RoomAddress,WorkPhone,HomePhone,Others:

And Others are divided in as many commas as you like:

:FullName,RoomAddress,WorkPhone,HomePhone,Other1,Other2,Other3:

In the man chfn pages one can read:

The other field is used to store accounting information used by other applications.

Now, for an application developer (I'm interested in C language, system calls and/or bash script) which is the best way to grab this information?

And considering just the Bash environment, given that finger command cannot display the others fields (or at least I don't see how), what are other commands that can? I know that chfn not only show, but allow them to be changed. What if one is to just output it to stdout?

kasperd
  • 2,931
DrBeco
  • 2,125

3 Answers3

4

The best way i have found is to use getent because that will work with LDAP/NIS or other methods of non local users

getent passwd $UID| awk -F ":" '{print $5}'
2

You can just use two nested cut commands:

  • first with ":" as the field separator, cut field 1 & 5; and
  • second with "," as the field separator, cut fields 1 & 5-N;
0x0065
  • 21
1

For example in a bash script you can print the fifth field of the file /etc/passwd with awk/gawk:

awk -F ":" '{print $5}' /etc/passwd

The option -F fs uses fs for the input field separator (in this case :).
You can read more, for example, on the GNU awk homepage [1].
Awk has the function split() to split a string (where in this case you will use the 5th field as string and the , as separator). Take insipiration from some other answer about it [2]....

Hastur
  • 19,483
  • 9
  • 55
  • 99