Script to Set Computer Name

devsutradhar19
New Contributor

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.

 

1 ACCEPTED SOLUTION

john_sherrod
Contributor II

Try this:

scutil <<< "show State:/Users/ConsoleUser" | awk '/kCGSSessionUserNameKey :/ && ! /loginwindow/ && ! /ladmin/ && ! /root/ { print $3 }' | cut -f1 -d'.'

View solution in original post

4 REPLIES 4

RaGL
New Contributor III

Instead of "firstName=$(finger -s $getUser | head -2 | tail -n1 | awk '{print toupper($2)}')'" you could use:

firstName=$(echo "$getUser" | cut -d. -f1)

 

john_sherrod
Contributor II

Try this:

scutil <<< "show State:/Users/ConsoleUser" | awk '/kCGSSessionUserNameKey :/ && ! /loginwindow/ && ! /ladmin/ && ! /root/ { print $3 }' | cut -f1 -d'.'

devsutradhar19
New Contributor

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'.'