Posted on 07-10-2024 03:29 AM
Hello All,
In our environment the Loggen in User name is in firstname.lastname format and I am trying to figure out how to extract the first name with a script. Till now what I have is below
#!/bin/bash
#gets current logged in user
getUser=$(ls -l /dev/console | awk '{ print $3 }')
Echo $getUser
#gets named
firstName=$(finger -s $getUser | head -2 | tail -n1 | awk '{print toupper($2)}')
computerName="ABCD-ABM-"$firstName"-MacBook"
Echo $computerName
#set all the name in all the places
scutil --set ComputerName "$computerName"
scutil --set LocalHostName "$computerName"
scutil --set HostName "$computerName"
sudo Jamf recon
And the result I am getting is -
ABCD-ABM-LASTNAME-MacBook
I am not good with scripting however what I understand is the 'firstName' is unable to fetch first name from firstname.lastname however same works good when the name is like firstname lastname.
Solved! Go to Solution.
Posted on 07-11-2024 05:39 AM
Try this:
scutil <<< "show State:/Users/ConsoleUser" | awk '/kCGSSessionUserNameKey :/ && ! /loginwindow/ && ! /ladmin/ && ! /root/ { print $3 }' | cut -f1 -d'.'
Posted on 07-10-2024 03:45 AM
Instead of "firstName=$(finger -s $getUser | head -2 | tail -n1 | awk '{print toupper($2)}')'" you could use:
firstName=$(echo "$getUser" | cut -d. -f1)
Posted on 07-11-2024 05:39 AM
Try this:
scutil <<< "show State:/Users/ConsoleUser" | awk '/kCGSSessionUserNameKey :/ && ! /loginwindow/ && ! /ladmin/ && ! /root/ { print $3 }' | cut -f1 -d'.'
Posted on 07-11-2024 07:26 AM
Thanks @john_sherrod
Working perfectly and I just did a small tweet to print to upper which was my requirement.
scutil <<< "show State:/Users/ConsoleUser" | awk '/kCGSSessionUserNameKey :/ && ! /loginwindow/ && ! /ladmin/ && ! /root/ { print toupper($3) }' | cut -f1 -d'.'
Posted on 07-11-2024 07:30 AM
Awesome!