4

This command works great to list the Full Name out:

finger $(whoami) | egrep -o 'Name: [a-zA-Z0-9 ]{1,}' | cut -d ':' -f 2 | xargs echo

But, if you run it after a

sudo su

It returns CVMS Root System Administrator

I'm wondering if there's perhaps another place to read the Full Name variable? Is there a User object somewhere that would list this out? I thought I was on to something with

dscacheutil -q user | grep -A 3 -B 2 -e uid:\ 5'[0-9][0-9]'

But it doesn't look like Full Name is represented in that return data.

2

You can use dscl to read the RealName:

$ dscl . -read /Users/grgarside RealName
RealName:
 George Garside

The following will give you just what you're after. This uses ‘who am i’ to get the username, then sed to format the output.

$ dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName | sed -n 's/^ //g;2p'
George Garside
  • Thanks! Got me close enough that I figured it out from there! – iMatthewCM Jan 16 '17 at 19:04
  • @Joodoo See edit, that's more streamlined. Also you should post your solutions as an actual answer rather than editing your question. – grg Jan 16 '17 at 19:23
  • @user ‘whoami’ returns root rather than the original user. Your awk command returns an empty line for me. – grg Jan 16 '17 at 19:38
  • @user Did you do ‘sudo dscl’? That doesn't run ‘whoami’ as root, only the dscl. Try ‘sudo whoami’ and it'll return root. – grg Jan 16 '17 at 19:40
  • 1
    That's really strange! I can't explain why we are getting different results. This part of it dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName works but the | sed -n 's/^ //g;2p' part returns nothing. – user3439894 Jan 17 '17 at 14:06
2

Simply use id -P $(stat -f%Su /dev/console) | awk -F '[:]' '{print $8}'

id -P $(stat -f%Su /dev/console) yields:

adminuser:********:501:20::0:0:AdminUser:/Users/adminuser:/bin/bash

awk -F '[:]' '{print $8}' yields the 8th term (the "RealName") of an output separated by colons.

As proposed by fd0 you can alternatively use:

id -P $(stat -f%Su /dev/console) | cut -d : -f 8

which is even more simple/elegant.

  • 1
    +1 for id -P though I would use cut instead of awk on the straight forward string. – fd0 Jan 17 '17 at 16:11
  • 1
    @fd0 Added as an alternative proposal. ;-) – klanomath Jan 17 '17 at 16:17
1

Needed a two-line approach:

username="$(stat -f%Su /dev/console)"
realname="$(dscl . -read /Users/$username RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$")"

And then you can just echo out the realname variable.

  • May I ask why you changed the accepted answer? Did you see the edit I had made to my answer? – grg Jan 28 '17 at 14:28
  • I did, but when I was working on this your edits probably weren't up yet, because I couldn't get your answer to work "out of the box." My answer is what I ended up actually implementing, so I know it works for my purposes. My original comment on your answer was "Thanks! Got me close enough that I figured it out from there!" which I stand by, since it did indeed give me the brain jumpstart I needed, so thank you very much for getting me on the right track :) – iMatthewCM Jan 31 '17 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .