I've been trying to adapt our computer naming script to wait for input from the user by cobbling together information fromhere and here and probably a few others, I can't seem to get it to work.
I just can't seem to figure out what I'm doing wrong, maybe there's an easier way? Can someone take a crack at it?
Original script that works (however if someone's not staring at the screen and they miss the prompts, then the names get messed up)
Also, if you can happen to fix the part at the top I commented out that looked at the machine type (laptop/desktop) and automatically puts in the correct type - that would be awesome :).
identifying data in script removed due to internal security requirements - but scrubbed version below retains the syntax and just made generic
1#!/bin/bash2set -x3# jamf recon4loggedInUser=$(stat -f%Su /dev/console)5loggedInUID=$(id -u $loggedInUser)67if [[ "$loggedInUser" != "root" ]] && [[ "$loggedInUser" != "_mbsetup" ]]; then8 ## Create local script9 cat << EOD > /private/tmp/computerrenamescript.sh10#!/bin/bash1112TT=$(/usr/bin/osascript -e 'tell application "System Events" to set MacTYPE to text returned of (display dialog "Please input the machine type - MD = Mac Desktop or ML = Mac Laptop" default answer "" with icon 2)')1314#ModelType=$(system_profiler SPHardwareDataType | grep 'Model Name:' | awk '{ print $3 }')15#echo "$ModelType"1617#if [[ "$ModelType" == "MacBook" ]]; then18# TT=ML19#else20# TT=MD21#fi222324BB=$(/usr/bin/osascript -e 'tell application "System Events" to set BRAND to text returned of (display dialog "Please input the brand - AA = AA, AB = AB, AC = AC," default answer "" with icon 2)')2526LL=$(/usr/bin/osascript -e 'tell application "System Events" to set MacLoc to text returned of (display dialog "Please enter the location - BA = BA, BB = BB, BC = BC" default answer "" with icon 2)')2728NN=$(/usr/bin/osascript -e 'tell application "System Events" to set DEPARTMENT to text returned of (display dialog "Please enter department - 01 = 01, 02 = 02, " default answer "" with icon 2)')2930SN=$(system_profiler SPHardwareDataType | grep 'Serial Number' | awk '{print $4}' | tail -c -8) #this grabs the last 7 of the serial number3132echo "${TT}${BB}${LL}${NN}${SN}" > /private/tmp/computerrenametext.txt3334EOD3536 ## Make script executable37 /bin/chmod +x /private/tmp/computerrenamescript.sh3839 ## Run the script as logged in user40 /bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" "/private/tmp/computerrenamescript.sh"4142 ## Get the new name from the local file43 newComputerName=$(cat /tmp/computerrenametext.txt)4445 if [ ! -z "$newComputerName" ]; then46 echo "$newComputerName"47 ## Rename the computer to the new name48 /usr/local/bin/jamf setComputerName -name "$newComputerName"49 echo $newComputerName50 scutil --set HostName $newComputerName51 scutil --set LocalHostName $newComputerName52 scutil --set ComputerName $newComputerName53 dscacheutil -flushcache54 jamf recon55 ## Remove local script56 rm -f /private/tmp/computerrenamescript.sh5758 exit 059 else60 echo "No name was found to rename to"6162 ## Remove local script63 rm -f /private/tmp/computerrenamescript.sh6465 exit 166 fi67else68 echo "No-one logged in. Exiting"69 exit 070fi