Skip to main content
Solved

Script request: Change full name to account name (System Pref/users & groups)

  • August 17, 2012
  • 29 replies
  • 130 views

Show first post

29 replies

Forum|alt.badge.img+8

@msblake

Thanks for the tip, but I'm not sure I have it correct?

#!/bin/sh

/usr/bin/who | awk '/console/{ print $1 }'

ShortName=/usr/bin/who | awk '/console/{ print $1 }'
FullName=dscl . -read /Users/$ShortName RealName | tail -1

else
dscl . -change /Users/$ShortName RealName "$FullName2" "$ShortName"
fi


Forum|alt.badge.img+8

@Johnny.Kim][/url][/url I did run it as a login trigger, and got this outcome: However, it did seem to work, I see the name is changed now...so we do have a solution, although it would be cool if I could do it to the current logged in user without a restart.

Executing Policy Reset full name...
Mounting Main Distribution Point to /Volumes/CasperShare...
Running script resetfullname.sh...
Script exit code: 54
Script result:
attribute status: eDSAttributeNotFound
DS Error: -14134 (eDSAttributeNotFound)


Forum|alt.badge.img+13
  • Author
  • Contributor
  • January 29, 2015

Steven, you're piping in "fullname2" but the variable isn't there. Copy and paste the code below and give that a try in terminal. run with "sudo bash -x -v /path/to/file"

ShortName=`/usr/bin/who | awk '/console/{ print $1 }'`
FullName=`dscl . -read /Users/$ShortName RealName | awk 'BEGIN {FS=": "} {print $1}'`
FullName2=`echo $FullName | awk '{print $2,$3}'`

if [ "$ShortName" == "$FullName2" ]; then
exit

else
dscl . -change /Users/$ShortName RealName "$FullName2" "$ShortName"
fi

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • January 29, 2015

I recommend switching the ShortName command to use:

ls -l /dev/console | awk '{print $3}'

The problem with the who | awk syntax is that if you happen to use Fast User Switching and have more than one account actually logged in, even if one is switched out, you'll get inaccurate results on the username. You could still run into problems even with the above command I recommend, but its only likely to happen in a case where another user is remoted into the Mac with ScreenSharing and "logged in" under another account, which probably doesn't happen much if at all.
But I agree that you should make your scripts as portable as possible and try not to rely on something like $3 exclusively. If you later decided to use that same script outside of a login or Self Service policy, you'd need to edit it to make it work. Starting off with something like:

ShortName=$(ls -l /dev/console | awk '{print $3}')

is going to serve you better long term, in my opinion.