|
|
#1 |
|
Prospect
Join Date: Jul 2008
Posts: 11
|
How can I get the user's long name?
On Tiger or Leopard (or any other Mac OSX), how can I obtain the user's "long name" from the system? Is there any command in Terminal.app that will return the user's long name, by itself or with other information? For example, I want to get "Dick Guertin" if that person's Desktop is the active Desktop.
|
|
|
|
|
|
#2 |
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,010
|
To get your own long name:
user=$(id -un) dscl . read /Users/$user RealName | tail -1 To get the long name of the user owning the GUI: user=$(stat -f%Su /dev/console) dcsl . read /Users/$user RealName | tail -1 |
|
|
|
|
|
#3 |
|
Prospect
Join Date: Jul 2008
Posts: 11
|
Thank you! I've search the web for this and couldn't find it. I've turned it into a script:
#!/bin/bash myname=$(dscl . read /Users/$USER RealName | tail -1) myname=${myname/RealName: /} echo "$myname" exit 0 |
|
|
|
|
|
#4 |
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,010
|
You can leave out the "| tail -1" part, then. On Lion, the output from the dscl command is on two lines, with "RealName:" on the first line and the the full name (with a leading space) on the second line. The "tail -1" part was to discard that first line.
But I see now that on Snow Leopard it comes out as one line, and the tail command does nothing interesting. Storing the output in a variable as you do (to normalize whitespace) and then deleting the "RealName: " prefix works on both Snow Leopard and Lion. I'll remind you that $USER is your username, not necessarily the username of the user whose Desktop is the active Desktop. (They can be different if you've ssh'ed into the computer, or if this is part of a launchd'ed background task.) You have to stat /dev/console to find out who owns the screen. |
|
|
|
|
|
#5 |
|
Prospect
Join Date: Jul 2008
Posts: 11
|
Leaving tail -1 allows it to work on Tiger through Lion. The deletion only happens on one-line responses, like Tiger and Leopard. $USER is sufficient for my needs, but anyone who needs to can change the script according to your prescription. Thanks again for pointing me in the right direction. Even the man-page for "dscl" doesn't show all the output fields, so I couldn't find this with "man -k ..." logic.
|
|
|
|
|
|
#6 |
|
Major Leaguer
Join Date: Apr 2005
Posts: 477
|
Here's a one-liner using a simple applescript command. You can run this from the command line and note that you can get the "short user name" instead if you wish. There's lots of useful information that you can pull from the command "get system info".
Code:
osascript -e "return (long user name of (get system info))" |
|
|
|
|
|
#7 |
|
Prospect
Join Date: Jul 2008
Posts: 11
|
Wonderful! An alternative method. Thanks Hank.
|
|
|
|
![]() |
| Tags |
| long name, user |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|