How to get a convenient username on UNIX?

I want to get a "friendly" name, not a username, at least if such a string exists for this user. Things I 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 

The latter has the information I'm looking for, but I would prefer not to understand it, especially because I'm not sure that the format (in particular, the number : s) will remain consistent between the OS.

+7
source share
5 answers

Parse the GECOS field for the full username

The format / etc / passwd and most of the GECOS field are very well standardized on Unix-like systems. If you find an exception, let us know. Meanwhile, the easiest way to get what you want in a Unix-like system is to use the gent and cut to analyze the GECOS field. For example:

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

The only way I know is to analyze it:

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

You can be sure of the format / etc / passwd

+2
source

You can use finger to get this information:

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

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

If you want to receive information only from /etc/passwd , you will most likely have to analyze the file one way or another, as mentioned earlier. Personally, I would prefer awk for this task:

 awk -F: -vid=`id -u` '{if ($3 == id) print $5}' /etc/passwd 
+2
source

Take a look at the /etc/passwd . This file shows how user information is stored. User information may or may not be saved here. (There are several different databases that Unix uses to store users), but the format is the same.

Basically, Unix uses a user identifier (UID) to store what a user is. The next entry was the old password entry, followed by the UID, the primary group identifier, GECOS , the $HOME directory, and the user shell. (There are three additional entries displayed in the id -P command on MacOS. I don't know what it is, but they make the GECOS field an eighth field instead of a fifth field).

Using the id -P command on your system, you received this entry. Some systems use getent or even getpwent as a command. What you need to do is parse this entry. Each field is separated by colons, so you need either the fifth or the eighth record (depending on the command you should have used).

The awk and cut commands do this pretty well. cut is probably more efficient, but awk more common, so I try to use it.

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

Using awk , you will get:

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

This means taking the id -P command and using : as a field separator and printing the eighth field. Skin braces surround all AWK programs, and single quotes are needed to interpret the $8 shell.

In BASH, you can use $( ) to run the command and return its output so that you can set the environment variables:

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

On macOS, at least (and possibly other * BSD-alikes), you can use: id -F to get only the full name.

0
source

All Articles