Posted on 07-07-2020 05:30 AM
I run a farm of 150 Mac minis. I am trying to create a first run script that will set the host name and static IP based on the SN. I am getting an end of file error when I am trying to run it. Here is a small sample. Any help would be great!
serial=/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'
if test "$serial" == "C07M802Z4E825DY3J"
then
scutil --set ComputerName "qa-mac-1"
scutil --set LocalHostName "qa-mac-1"
networksetup -setproxyautodiscovery "Ethernet" on
networksetup -setmanual Ethernet 10.1.1.1 255.255.255.128 10.1.1.129
networksetup -setdnsservers Ethernet 10.2.76.98 10.2.76.97
networksetup -setsearchdomains Ethernet mycompany.com mycompanycorp.com us.mycompany.com
else
if test "$serial" == "C07M803JDLSY3J"
then
scutil --set ComputerName "qa-mac-2"
scutil --set LocalHostName "qa-mac-2"
networksetup -setproxyautodiscovery "Ethernet" on
networksetup -setmanual Ethernet 10.1.1.2 255.255.255.128 10.1.1.129
networksetup -setdnsservers Ethernet 10.2.76.98 10.2.76.97
networksetup -setsearchdomains Ethernet mycompany.com mycompanycorp.com us.mycompany.com
if test "$serial" == "C0737951JDLSY3J"
then
scutil --set ComputerName "qa-mac-3"
scutil --set LocalHostName "qa-mac-3"
networksetup -setproxyautodiscovery "Ethernet" on
networksetup -setmanual Ethernet 10.1.1.2 255.255.255.128 10.1.1.129
networksetup -setdnsservers Ethernet 10.2.76.98 10.2.76.97
networksetup -setsearchdomains Ethernet mycompany.com mycompanycorp.com us.mycompany.com
fi
exit 0
Posted on 07-07-2020 06:21 AM
Use elif
instead of else if
. Otherwise you are creating a bunch if blocks, but you are not closing the if blocks with fi
statements. Try writing it like this:
#!/bin/sh
if [ $serial == "<<SERIAL1>>" ]; then
<<code for SERIAL1>>
elif [ $serial == "<<SERIAL2>>" ]; then
<<code for SERIAL2>>
elif [ $serial == "<<SERIAL3>>" ]; then
.
.
.
fi
exit 0
If you need to look for errors in your code, try Shellcheck. It can help identify errors in your code.
Posted on 07-07-2020 10:03 AM
Thanks that worked!