I think all you'd need to do is grep the name of the user folder, assign it to a variable and then use the standard rename commands in a script to assign those names to the value of that variable.
You can try by querying the current logged in username and model identifier. $loggedInUser may be different before the user is logged in so test it.
#!/bin/sh
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
getmodel=`sysctl -n hw.model`
if [[ $getmodel == *"MacBookPro"* ]]; then
scutil --set HostName "$loggedInUser-mbp"
scutil --set LocalHostName "$loggedInUser-mbp"
scutil --set ComputerName "$loggedInUser-mbp"
elif [[ $getmodel == *"MacBookAir"* ]]; then
scutil --set HostName "$loggedInUser-mba"
scutil --set LocalHostName "$loggedInUser-mba"
scutil --set ComputerName "$loggedInUser-mba"
fi
You can actually instruct the script to wait until a user is logged-in with this little function. Add this to the start of the above script and you should be good to go, but do test it first as always!
function wait_for_user () {
# Wait for the dock to determine the current user
DOCK_STATUS=$(pgrep -x Dock)
echo "Waiting for Desktop..."
while [[ "$DOCK_STATUS" == "" ]]; do
echo "Desktop is not loaded; waiting..."
sleep 5
DOCK_STATUS=$(pgrep -x Dock)
done
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
echo "$loggedInUser is logged in and at the desktop; continuing."
}
wait_for_user
Naturally, that function can be used for anything you want to run only when a user is actually logged-in to the Mac.
Remember that you'll need to still declare $loggedInUser outside of the function, because variables declared within functions only exist within the function. Therefore, you shouldn't need to change @karthikeyan_mac's script at all underneath the 'wait_for_user' function.
You can try by querying the current logged in username and model identifier. $loggedInUser may be different before the user is logged in so test it.
#!/bin/sh
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
getmodel=`sysctl -n hw.model`
if [[ $getmodel == *"MacBookPro"* ]]; then
scutil --set HostName "$loggedInUser-mbp"
scutil --set LocalHostName "$loggedInUser-mbp"
scutil --set ComputerName "$loggedInUser-mbp"
elif [[ $getmodel == *"MacBookAir"* ]]; then
scutil --set HostName "$loggedInUser-mba"
scutil --set LocalHostName "$loggedInUser-mba"
scutil --set ComputerName "$loggedInUser-mba"
fi
just testing the above i get the following
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
awk: syntax error at source line 1
context is
/Name 😕😕 && ! /loginwindow/ >>> { <<<
You can try by querying the current logged in username and model identifier. $loggedInUser may be different before the user is logged in so test it.
#!/bin/sh
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
getmodel=`sysctl -n hw.model`
if [[ $getmodel == *"MacBookPro"* ]]; then
scutil --set HostName "$loggedInUser-mbp"
scutil --set LocalHostName "$loggedInUser-mbp"
scutil --set ComputerName "$loggedInUser-mbp"
elif [[ $getmodel == *"MacBookAir"* ]]; then
scutil --set HostName "$loggedInUser-mba"
scutil --set LocalHostName "$loggedInUser-mba"
scutil --set ComputerName "$loggedInUser-mba"
fi
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
awk: syntax error at source line 1
context is
/Name 😕😕 && ! /loginwindow/ >>> { <<<
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
awk: syntax error at source line 1
context is
/Name 😕😕 && ! /loginwindow/ >>> { <<<
Attaching as screenshot. 
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕😕 && ! /loginwindow/ { print $3 }' )
awk: syntax error at source line 1
context is
/Name 😕😕 && ! /loginwindow/ >>> { <<<
Also, try this instead if that doesn't work:
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
#!/bin/sh
originalName=$(networksetup -getcomputername)
echo "Computer name is currently $originalName"
# Create function to wait for the Dock to launch to confirm a user is logged-in
function wait_for_user () {
DOCK_STATUS=$(pgrep -x Dock)
echo "Waiting for Desktop..."
while [[ "$DOCK_STATUS" == "" ]]; do
echo "Desktop is not loaded; waiting..."
sleep 5
DOCK_STATUS=$(pgrep -x Dock)
done
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name && ! /loginwindow/ { print $3 }' )
echo "$loggedInUser is logged in and at the desktop; continuing."
}
# Calls the above function you just created
wait_for_user
# Get currently logged-in user and assign to variable
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name && ! /loginwindow/ { print $3 }' )
echo "Current user is $loggedInUser"
# Get Mac model and assign to variable
getModel=$(sysctl -n hw.model)
echo "Detected Mac model is $getModel"
# Put the two together and rename in the format 'username-macmodel'
if [[ $getModel == *"MacBookPro"* ]]; then
scutil --set HostName "$loggedInUser-mbp"
scutil --set LocalHostName "$loggedInUser-mbp"
scutil --set ComputerName "$loggedInUser-mbp"
elif [[ $getModel == *"MacBookAir"* ]]; then
scutil --set HostName "$loggedInUser-mba"
scutil --set LocalHostName "$loggedInUser-mba"
scutil --set ComputerName "$loggedInUser-mba"
fi
newName=$(networksetup -getcomputername)
echo "Hostname, Local name and Computer name are set to $newName"
if [[ $originalName == $newName ]]; then
echo "New name is the same as the original name, script failed"
exit 1
else
echo "Names changed successfully!"
exit 0
fi
This should be the full script. I've added comments so you/others can see what parts are doing what, and it makes it easier to troubleshoot if something goes wrong as there are echo commands for the various parts too. Please try it before pushing it!
I can't seem to do a lot about it changing the colon-forward slash to a 😕😕 so do bear that in mind.