Bash Script to Compare Hostname from System and System Prefrences

New Contributor II

I am trying to write a script that will be an extension attribute that will check if the system hostname (scuitl --get HostName) is the same as the hostname that pops up in system preferences (scutil --get ComputerName).

This is the Bash Script I have been messing with and if you run it you can see the echos will literally show that the outputs are the exact same. But yet the if statement says they are different.

What am I doing wrong? Am I using the wrong condition? Or are the variables actually different someway on the back end?

The script in question

var1=$(scutil --get ComputerName) var2=$(scutil --get HostName) echo $var1 echo $var2 if [ "$var1" == "$var2" ]; then echo same echo "<result>Same</result>" else echo different echo "<result>Different</result>" fi

The script I am using to change the hostname, in a policy, that will scope to a smart group set by the attribute of the above script.

#set variable to system prefrences name oldHostName=$(scutil --get HostName) spHostName=$(scutil --get LocalHostName) echo " System Prefrences Hostname is: $spHostName " echo " Current System Hostname is: $oldHostName "

change hostname system wide to LocalHostName

echo " Now setting sytem hostname to: $spHostName ... " sudo scutil --set HostName $spHostName echo "Done!" newHostName=$(scutil --get HostName) echo " System Hostname is now: $newHostName" echo " Now Exiting..." exit

Valued Contributor

Are you sure you don't have any leading or trailing blanks or any other non-printing characters in any of the names? To detect issues like that I often do a "echo ${var1}".

New Contributor III

Yeah almost sounds like trailing or leading space. White spaces can be strict if you pipe the scutil command into sed -e 's/^[ ]*//'