The "other" finger (GECOS fields at /etc/passwd)

6

1

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?

Dr Beco

Posted 2016-01-26T16:34:41.177

Reputation: 1 277

For example in bash you can extract the field with awk -F ":" '{print $5}' /etc/passwd ... then you can process again the string (you can do with a single call too via splitting the field with the split function. – Hastur – 2016-01-26T16:45:28.863

I was wondering if I could use a command specific to the job, instead of text processing commands (awk, sed, cat, grep, cut, and alike). Also, how applications would read this? Any example of an application that do use the other field? – Dr Beco – 2016-01-26T17:05:39.280

Answers

3

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}'

Tim Hughes

Posted 2016-01-26T16:34:41.177

Reputation: 146

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

Posted 2016-01-26T16:34:41.177

Reputation: 15 043

This command would give the whole GECOS field. But its a starting point for an answer. – Dr Beco – 2016-01-26T17:03:58.247

This "just output to the stdout". From here, using the tip of the split () function you can start to write your script... – Hastur – 2016-01-26T17:11:05.207