set device name as assigned user instead of logged on user

Contributor II

Ive always had issues with device naming, our preference is to set the device device name as the assigned username, for our 1to1 devices, as our usernames are unique. we add a prefix D-20 to indicate that its a DEP device and the year of its enrolment - which helps when it comes to using smart groups to target specific devices

After a lot of headaches it was later discovered theres no existing variable feature in Jamf pro to call on this information into a script from jamf pro for macOS, and due to the sheer impact of work load this adds to our deployments and a intent to move to 0 touch for covid reasons, I've recently come up with a solution using the jamf api.

Below is a command I am working with as a script to set the assigned user as a variable and then set the machine name - I hope to use some extra API to set the prefix information in the near future too, based on the prestage and then develop a similar script for manual enrolment.
I'm not a programmer, and some of this was adopted from other API posts on here, combined with a bit of shell command research, so some of this could possibly be written more efficiently. what I found interesting about the curl command run in this manner, was it output the result in one line of text, where as using the API provided one, which is uniquely generated for 15 minute duration of password auth, would export as a proper xml format. I suspect its a curl extension command, but I couldn't figure out why...
below is the script if any one needs it or wishes to modify it.
The user it uses to do the api lookup is set as read only. strongly suggest not to use anything other. But I also welcome suggestions to improve it, even possibly remove the need of auth would be great.

#! /bin/bash
#get serial number
strmacbookserial=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'|tr -d '"'`
#get assigned user
strassigneduser=`/usr/bin/curl -s -u "USERNAME":"PASSWORD" -X GET "https://MDMHOSTADDRESS:8443/JSSResource/computers/match/$strmacbookserial" -H "accept: application/xml" | awk -F'\>\<' '{$1=$1}1' OFS='
' | grep username | sed -e 's/username>//g' | sed -e 's.</username..g'`
#setusername to uppercase
strmacbookname=`echo D-20-$strassigneduser|tr '[a-z]' '[A-Z]'`
sudo jamf displayMessage -message "macbookname: $strmacbookname"

sudo scutil --set ComputerName $strmacbookname
sudo scutil --set LocalHostName $strmacbookname
sudo scutil --set HostName $strmacbookname