How to rename a mac based on its hardware model via a script

Discussion in 'OS X Mountain Lion (10.8)' started by kildjean, Nov 4, 2013.

  1. kildjean, Nov 4, 2013
    Last edited: Nov 4, 2013

    kildjean macrumors regular

    kildjean

    Joined:
    Apr 16, 2005
    Location:
    Useless, TX
    #1
    Hi all;

    What I am trying to do is a script that does the following:

    1. reads what mac model it is (MacBookPro10,1) and use MacBookPro as an identifier... (or MacPro8,1) and use MacPro.

    Then based on that name the computer if its a laptop ML-COMPUTERSERIAL or desktop/imac MW-COMPUTERSERIAL

    I am pretty sure the code before the If/then Else is ok, its the IF/then that I am having trouble with so if someone could look at it and show me how to do it or clean that part up I would appreciate it...

    So far I have the following:

    Code:
    #!/bin/bash
    laptop="ML-"
    workstation="MW-”
    tld="yourdomain.com"
    serial=`ioreg -l | awk '/IOPlatformSerialNumber/ { split($0, line, "\""); printf("%s\n", line[4]); }'`
    model=`ioreg -l | awk '/product-name/ { split($0, line, "\""); printf("%s\n", line[4]); }'
    
    if ["$model”=="$MacBookPro*” ]; then :laptop 
    
    :laptop
    /usr/sbin/scutil --set ComputerName $laptop$serial
    /usr/sbin/scutil --set LocalHostName $laptop$serial
    /usr/sbin/scutil --set HostName "${laptop}${serial}.${tld}"
    
    else 
    
    ["$model”==”$MacPro*” ]; then 
    :workstation
    /usr/sbin/scutil --set ComputerName $workstation$serial
    /usr/sbin/scutil --set LocalHostName $workstation$serial
    /usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
    
    If you can't read the whole code you can read it here better:
    http://slexy.org/view/s2166IItwi
     
  2. MacUser2525, Nov 4, 2013
    Last edited: Nov 4, 2013

    MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #2

    When you use an else statement there is no need for a then, the else part is what you want to happen by default if the if is not true.

    Code:
    if some condition; then
         do this
         else
              do this
    fi
    
    So it makes :laptop & :workstation in your script redundant. Also where are you getting the variables $base, $MacPro, $MacBookPro from.

    Edit: Just messed around a bit and came up with this which I think does what you want.

    Code:
    #!/bin/bash
    laptop="ML-"
    workstation="MW-”
    tld="yourdomain.com"
    serial=`ioreg -l |grep "IOPlatformSerialNumber"|cut -d ""="" -f 2|sed -e 's/[^[:alnum:]]//g'`
    model=`ioreg -l |grep "product-name" |cut -d ""="" -f 2|sed -e 's/[^[:alnum:]]//g' | sed 's/[0-9]//g'`
    
    if ["$model”=="MacBookPro" ]; then  
         /usr/sbin/scutil --set ComputerName $laptop$serial
         /usr/sbin/scutil --set LocalHostName $laptop$serial
         /usr/sbin/scutil --set HostName "${laptop}${serial}.${tld}"
    
         else 
              /usr/sbin/scutil --set ComputerName $workstation$serial
              /usr/sbin/scutil --set LocalHostName $workstation$serial
             /usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
    fi
    
     
  3. kildjean thread starter macrumors regular

    kildjean

    Joined:
    Apr 16, 2005
    Location:
    Useless, TX
    #3
    The original code had a typo. I have fixed above, and to help you understand better please read below:

    Code:
    laptop="ML-"
    workstation="MW-”
    tld="yourdomain.com"
    serial=`ioreg -l | awk '/IOPlatformSerialNumber/ { split($0, line, "\""); printf("%s\n", line[4]); }'`
    model=`ioreg -l | awk '/product-name/ { split($0, line, "\""); printf("%s\n", line[4]); }'
    
    laptop variable is to identify the prefix for laptops which is "ML-"
    workstation variable is "MW-"
    tld= domain

    The line: serial=`ioreg -l | awk '/IOPlatformSerialNumber/ { split($0, line, "\""); printf("%s\n", line[4]); }'` is for grabbing the serial number of the mac.

    The line: model=`ioreg -l | awk '/product-name/ { split($0, line, "\""); printf("%s\n", line[4]); }' is for grabbing the model.

    depending what the model line identifies your mac as, it should be declared as the $MacBookPro or $MacPro

    I don't know how to make the model code == $MacBookPro

    that part if what I am trying to figure out... how after the line above model=io.. etc I get the model and if its MacBookPro to assign the code to name the laptop ML-SERIALNUMBER

    Sorry not trying to confuse you, just to explain... I don't know unix shell script too well, still learning.
     
  4. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #4
    Give what I posted in my edit a try.
     
  5. kildjean thread starter macrumors regular

    kildjean

    Joined:
    Apr 16, 2005
    Location:
    Useless, TX
    #5
    I gave it a try but it didn't work:
    [​IMG]

    But I know why it failed, I forgot to mention we have imacs in the environment, so there has to be a field for IMAC as well as Mac Pro and MacBookPro

    Oops.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    It failed because you have "curly" quotes instead of "straight" ones.

    Your original code (which I've given a larger size) has curly quotes:
    Code:
    [SIZE="5"]if ["$model[COLOR="Red"]”[/COLOR]=="$MacBookPro*[COLOR="red"]”[/COLOR] ]; then :laptop 
    [/SIZE]
    I've also hilited them in red.

    You need to go into your shell-script file and replace every curly double-quote (”) with a straight double-quote ("). This will at least make the quoting correct.

    Then you need to follow MacUser2525's advice about how to correctly write an if/then/else/fi structure. I will also point out that MacUser2525's posted code has curly quotes in it, so if you copy and paste it exactly, it will fail.

    If it still doesn't work after making the changes, then post the revised code along with any error messages. It's not necessary to post screen shots; you can copy and paste text from the Terminal window.

    Finally, MacUser2525's code will treat an iMac (or any computer not a MacBookPro) as a workstation, so it will get a "MW-" name. This is the nature of if/else: anything not covered by the 'if' condition will necessarily fall under the else.
     
  7. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #7
    Damn those don't show up here in the reply box or I did not notice new improved version below.

    Code:
    #!/bin/bash
    laptop="ML-"
    workstation="MW-"
    tld="yourdomain.com"
    serial=`ioreg -l |grep "IOPlatformSerialNumber"|cut -d ""="" -f 2|sed -e s/[^[:alnum:]]//g`
    model=`ioreg -l |grep "product-name" |cut -d ""="" -f 2|sed -e s/[^[:alnum:]]//g | sed s/[0-9]//g`
    
    
    case "$model" in
    	"MacPro" )
    		#/usr/sbin/scutil --set ComputerName $workstation$serial
    		#/usr/sbin/scutil --set LocalHostName $workstation$serial
    		#/usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
    		echo $model
    		echo $workstation$serial
    		echo "${workstation}${serial}.${tld}"
    		;;
    	"MacBookPro" )
    		#/usr/sbin/scutil --set ComputerName $laptop$serial
    		#/usr/sbin/scutil --set LocalHostName $laptop$serial
    		#/usr/sbin/scutil --set HostName "${laptop}${serial}.${tld}"
    		;;
    	"iMac" )
    		#whatever you need to do for iMac here
    		;;
    esac
    
    As the OP can see I just echo the values first to test the output always a good idea on a working system lest you screw it up with unexpected results. You would need to remove the echo lines and the # in front of the commands for it to work as you intend.
     
  8. kildjean thread starter macrumors regular

    kildjean

    Joined:
    Apr 16, 2005
    Location:
    Useless, TX
    #8
    It took me a while to understand how you did it (that and about 100 trouble tickets) ;) but I took your script and I cleaned it up a bit more and its working perfect!!

    Code:
    #!/bin/bash
    
    laptop="ML-"
    workstation="MW-"
    server="S-"
    tld="yourdomain.com"
    serial=$(ioreg -l |grep "IOPlatformSerialNumber"|cut -d ""="" -f 2|sed -e s/[^[:alnum:]]//g)
    model=$(ioreg -l |grep "product-name" |cut -d ""="" -f 2|sed -e s/[^[:alnum:]]//g | sed s/[0-9]//g)
    
    case "$model" in
    				"MacBookPro" )
    				/usr/sbin/scutil --set ComputerName "$laptop$serial"
    				/usr/sbin/scutil --set LocalHostName "$laptop$serial"
    				/usr/sbin/scutil --set HostName "${laptop}${serial}.${tad}"
    				echo "$model"
    				echo "$laptop$serial"
    				echo "${laptop}${serial}.${tld}"
    				;;
    				"MacBookAir" )
    				/usr/sbin/scutil --set ComputerName "$laptop$serial"
    				/usr/sbin/scutil --set LocalHostName "$laptop$serial"
    				/usr/sbin/scutil --set HostName "${laptop}${serial}.${tad}"
    				echo "$model"
    				echo "$laptop$serial"
    				echo "${laptop}${serial}.${tld}"
    				;;
    				"MacPro" )
    				/usr/sbin/scutil --set ComputerName "$workstation$serial"
    				/usr/sbin/scutil --set LocalHostName "$workstation$serial"
    				/usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
    				echo "$model"
    				echo "$workstation$serial"
    				echo "${workstation}${serial}.${tld}"
    				;;
    				"iMac" )
    				/usr/sbin/scutil --set ComputerName "$workstation$serial"
    				/usr/sbin/scutil --set LocalHostName "$workstation$serial"
    				/usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
    				echo "$model"
    				echo "$workstation$serial"
    				echo "${workstation}${serial}.${tld}"
    				;;
    				"Macmini" )
    				/usr/sbin/scutil --set ComputerName "$server$serial"
    				/usr/sbin/scutil --set LocalHostName "$server$serial"
    				/usr/sbin/scutil --set HostName "${server}${serial}.${tld}"
    				echo "$model"
    				echo "$server$serial"
    				echo "${server}${serial}.${tld}"
    	
    esac
    
    I really appreciate your help, because you taught me to do what I wanted differently than what I originally thought I needed to do... I thought If/Else was the best route and by doing 'case' it worked even better...

    ----------

    Now, If I wanted to add a line that went, "If no computer model is found, then echo "Computer model not found." then it exits...

    is there a way to say if case =! $model then exit ?
     
  9. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #9
    From the page below which I found with a search on " case statement exit on no match" looks like a:

    Code:
          * )
             echo "Computer model not found."
             exit 0
          ;;
    
    As the last entry will do what you want as it will match anything not defined in your cases above it.

    http://www.thegeekstuff.com/2010/07/bash-case-statement/
     

Share This Page