Get Currently Logged In Username From Active Directory Using Terminal – Mac OS X

#!/bin/bash
 
# gets login name
uname=$( ls -l /dev/console | awk '{ print $3 }' ) 
 
# gets first name
first=$( dscl . read /Users/$uname RealName | awk '{ print $2 }' ) 
 
# gets last name with a , at the end
last=$( dscl . read /Users/$uname RealName | awk '{ print $1 }' | grep -v RealName: ) 
 
lastname=$( echo ${last%?} )
 
# puts firstname lastname into variable full
full=$( echo "$first"-"$last" )
 
first=$( echo $full | cut -d - -f1 )
 
#removes , from the end
final=$( echo ${full%?} )

Leave a Reply

Your email address will not be published. Required fields are marked *

*