Subscribe
# get the current computer name
NAME=`scutil --get ComputerName`
# split the name into its component parts
BLDG=`echo $NAME | awk -F_ '{print $1}'`
CPU=`echo $NAME | awk -F_ '{print $2}'`
MOD=`echo $NAME | awk -F_ '{print $3}'`
NUM=`echo $NAME | awk -F_ '{print $4}'`
# replace full building name with abbreviation
newBLDG=`echo $BLDG | sed \
-e 's/Berkeley/berk/' \
-e 's/Washington/wash/'`
# make model lowercase
newMOD=`echo $MOD | tr [:upper:] [:lower:]`
# replace cpu speed with letter (or no letter for 700MHz)
newCPU=`echo $CPU | sed \
-e 's/1.42GHz/A/' \
-e 's/1.25GHz/B/' \
-e 's/700MHz//'`
# reassemble name from new parts
newNAME=$newBLDG-$newMOD-$newCPU$NUM
# assign the new name to the computer
scutil --set ComputerName $newNAME
scutil --set LocalHostName $newNAME
$(...) is better than `...` for a couple of reasons. First, it looks clearer and is harder to get confused with parentheses. Second, it means you can nest command substitutions if you want. Third, it means you don't have to have a confusing extra level of backslashes if you're using backslashes.cut -d _ -f 1 instead of awk -F_ '{print $1}'. Most efficiently would be to do it all in one go using perl.echo Berkeley_700Mhz_eMac_01 |
awk -F_ '{ print substr(tolower($1), 0, 4) "-" tolower($3) "-" ($2 == "700MHz" ? "" : ($2 == "1.42Ghz" ? "A" : ($2 == "1.25Ghz" ? "B" : $2))) "-" $4}'
eval $(echo $NAME | awk -F_ '{print "BLDG=" $1 ";CPU=" $2 ";MOD=" $3 ";NUM=" $4 }')eval $(scutil --get ComputerName | awk -F_ '
{ print "BLDG=" tolower(substr($1,1,4)) ";CPU=" $2 ";MOD=" tolower($3) ";NUM=" $4 }
')NAME=$(scutil --get ComputerName | awk -F_ '{
if( $2 == "1.42GHz" ) { x = "A" } else if( $2 == "1.25GHz" ) x = "B"
print tolower(substr($1,1,4)) "-" tolower($3) "-" x "-" $4
}')
scutil --set ComputerName $NAME
scutil --set LocalHostName $NAMEYou are not logged in, either login or create an account to post comments
posted by andrew cooke at 7:10 AM on August 29, 2007