Skip to main content
Solved

Script to Set Computer Name


Forum|alt.badge.img+2

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.

 

Best answer by john_sherrod

Try this:

scutil <<< "show State:/Users/ConsoleUser" | awk '/kCGSSessionUserNameKey :/ && ! /loginwindow/ && ! /ladmin/ && ! /root/ { print $3 }' | cut -f1 -d'.'
View original
Did this topic help you find an answer to your question?

4 replies

RaGL
Forum|alt.badge.img+7
  • Contributor
  • 36 replies
  • July 10, 2024

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
Forum|alt.badge.img+17
  • Valued Contributor
  • 104 replies
  • Answer
  • July 11, 2024

Try this:

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

Forum|alt.badge.img+2

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

john_sherrod
Forum|alt.badge.img+17
  • Valued Contributor
  • 104 replies
  • July 11, 2024
devsutradhar19 wrote:

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

Awesome!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings