Determine Computer name

Similarly to how I just asked about finding out the WiFi name, I'd also like to script out how to find the computer name. Not the results of 'uname -n' but what is reflected in System Preferences > Sharing. I was able to do it but I'm certain there is a better way- I'm just not familiar enough with sed, awk, etc to do it efficiently. Also, there might be another command altogether that does the trick.

My command ultimately is
$ cat /Library/Preferences/SystemConfiguration/preferences.plist|awk '/ComputerName/ {getline;print}'|awk -F'[<|>]' '/string/{print $3}'
mymacbookpro


Towards the bottom of preferences.plist is the computer name. It's in XML underneath the line containing ComputerName.

###
### Explanation
###
### Print the lines after any occurance of "ComputerName"
$ cat /Library/Preferences/SystemConfiguration/preferences.plist|awk '/ComputerName/ {getline;print}'
<string>mymacbookpro</string>
<integer>0</integer>
### Remove XML/HTML-ish tags, which also only prints the first instance (not sure why)
$ cat /Library/Preferences/SystemConfiguration/preferences.plist|awk '/ComputerName/ {getline;print}'|awk -F'[<|>]' '/string/{print $3}'
mymacbookpro


Message was edited by: ai4891
I was able to shorten it from the original but I still think there's room for improvement, any ideas?

Posted on Mar 11, 2009 8:21 PM

Reply
6 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Determine Computer name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.