Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 9 votes, 5.00 average. Display Modes
Old 05-12-2012, 03:38 PM   #1
dickguertin
Prospect
 
Join Date: Jul 2008
Posts: 13
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.
dickguertin is offline   Reply With Quote
Old 05-12-2012, 04:28 PM   #2
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,191
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
ganbustein is offline   Reply With Quote
Old 05-12-2012, 04:47 PM   #3
dickguertin
Prospect
 
Join Date: Jul 2008
Posts: 13
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
dickguertin is offline   Reply With Quote
Old 05-12-2012, 05:22 PM   #4
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,191
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.
ganbustein is offline   Reply With Quote
Old 05-12-2012, 06:41 PM   #5
dickguertin
Prospect
 
Join Date: Jul 2008
Posts: 13
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.
dickguertin is offline   Reply With Quote
Old 05-12-2012, 07:12 PM   #6
regulus6633
Major Leaguer
 
Join Date: Apr 2005
Posts: 478
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))"
__________________
Hank
http://www.hamsoftengineering.com
regulus6633 is offline   Reply With Quote
Old 05-13-2012, 12:58 AM   #7
dickguertin
Prospect
 
Join Date: Jul 2008
Posts: 13
Wonderful! An alternative method. Thanks Hank.
dickguertin is offline   Reply With Quote
Reply

Tags
long name, user

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 06:21 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.