The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   Command to retrieve computer name? (not hostname) (http://hintsforums.macworld.com/showthread.php?t=101493)

Hal Itosis 12-11-2009 10:58 PM

commands to retrieve computer name
 
FWIW, i pieced together this script to quickly run through all [eight!] possibilities, so we can quickly compare their various outputs. I even added another approach that uses AppleScript (via osascript). So far, i think scutil seems the simplest. Before you discount the PlistBuddy method, it's the only one that works in [raw] single-user mode... though i'm not sure there's much use for knowing the computer name there.

Code:

#!/bin/bash -
# compare eight methods for getting the "Computer Name"
IFS=$' \t\n'
declare -x PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/libexec

function show () { printf '\n\e[1m%s\e[0m\n' "$1"; }

show 'hostname -s'
hostname -s

show 'scutil --get ComputerName'
scutil --get ComputerName

show "uname -n |sed 's/\..*$//'"
uname -n |sed 's/\..*$//'

show "sysctl -n kern.hostname |sed 's/\..*$//'"
sysctl -n kern.hostname |sed 's/\..*$//'

show 'networksetup -getcomputername 2>/dev/null'
networksetup -getcomputername 2>/dev/null

show "osascript -e 'computer name of (system info)'"
osascript -e 'computer name of (system info)'

show "system_profiler SPSoftwareDataType |sed '/ *Computer Name: /!d;s///'"
system_profiler SPSoftwareDataType |sed '/ *Computer Name: /!d;s///'

show "PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' \\
/Library/Preferences/SystemConfiguration/preferences.plist"
PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' \
/Library/Preferences/SystemConfiguration/preferences.plist

echo
exit


tlarkin 12-12-2009 01:18 AM

Hal-

You forgot a method, there is also a switch in the systemsetup command to get and set the computer name.

Shardy 12-12-2009 07:38 AM

Well personally being a bit "old school" and a long time Unix head I'd use this...

uname -n | awk -F. '{ print $1 }'

:D

Hal Itosis 12-12-2009 03:08 PM

Hmm, interesting and weird: it pops up a *GUI* dialog requesting a password!!! :eek:

Think i'll skip that one. (it appears to be networksetup's little brother anyway).

renaultssoftware 01-03-2010 07:32 PM

Must be some OSA script:
Code:

osascript -e 'computer name of (system info)'
This is the simplest way.

Hal Itosis 01-03-2010 11:53 PM

I don't understand what's going on here. I visited this thread several times since posting my 12-12-2009, 03:08 PM reply (post #24) to tlarkin. The post by Shardy was never here then (so in fact, my post was actually numbered #23 when i replied back in Dec.). And it remained so for a while when i checked back. [i.e., no Shardy post.]

Now both Shardy and renaultssoftware are repeating stuff already contained in my post #21. It's almost as if they (at times) can't see my posts and i (at times) can't see theirs.

Maybe it's that thing where new member posts don't display until they're approved? If so, that would be *my* excuse then.

NovaScotian 01-04-2010 09:22 AM

Perhaps the Mods merged two threads.

anika123 01-04-2010 11:54 AM

Hal, I just now saw your post after visiting this thread several times.


All times are GMT -5. The time now is 10:19 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.