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

9,997

Solution 1

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

Solution 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;

Solution 3

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]....

Share:
9,997

Related videos on Youtube

DrBeco
Author by

DrBeco

Computer Scientist, Cognitive Scientist, Philosophy of Mind, Painter, Poet, Biker and Judoka (that I remember) I just created a nice clone (fork actually) of the Urban Muller language, called BrainForce (now I can keep my job as professor and still uses the language to teach Turing Machines and nice properties). Still don't have a website for it, but I can mail you if you want. All source code available. Translate BF to C. The release is at your disposal here. Lots of options to generate different code for various situations. Blog Beco cc

Updated on September 18, 2022

Comments

  • DrBeco
    DrBeco over 1 year

    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?

    • Hastur
      Hastur over 8 years
      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.
    • DrBeco
      DrBeco over 8 years
      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?
  • DrBeco
    DrBeco over 8 years
    This command would give the whole GECOS field. But its a starting point for an answer.
  • Hastur
    Hastur over 8 years
    This "just output to the stdout". From here, using the tip of the split () function you can start to write your script...
  • Mikko Rantalainen
    Mikko Rantalainen about 4 years
    If you just want the user's name, you can do getent passwd $UID | cut -d: -f5 | cut -d, -f1 (basically this says: cut the 5th field separated by : and then cut the 1st field separated by ,).