Search code examples
bashmacosmacos-mojave

macOS: How to set install volume to a variable using ./startosinstall


For my testing purposes I have to do installs of macOS on a lot of different machines. I've been hardcoding the desired install volume into my start up script. But I'd love to set the volume path to a variable. I get Error:

could not find target.

while attempting to run the script - some irrelevant parts have been omitted.

#!/bin/bash

Boot= diskutil info / | grep "Volume Name:" | awk '{print $3}'
echo $Boot
./startosinstall --volume /Volumes/$Boot --agreetolicense --rebootdelay 200

Solution

  • There are several problems with how you're getting and using the volume name, but before I get to them: why do you need it at all? I haven't used it, but my understanding is that startosinstall defaults to installing onto the current startup volume, so you can just leave the --volume option off:

    ./startosinstall --agreetolicense --rebootdelay 200
    

    Now, if you actually do need the startup volume name, here's what needs fixing:

    • To capture the output of a command in a variable, you need to use var=$(command). You're missing the $( ), and there cannot be a space after the = (or before it either). (BTW, there's a version that uses backquotes instead of $( ), but it's messier in a couple of ways, so don't use it.)

    • The command diskutil info / | grep "Volume Name:" | awk '{print $3}' will print the first word of the startup volume's name. For example, if the volume is named "Macintosh HD", the output of the diskutil | grep part is "Volume Name: Macintosh HD", and awk will print the third word of that, which is "Macintosh". Fixing this is messier; the simplest way that occurs to me is to use sed instead of grep and awk:

      Boot=$(diskutil info / | sed -n 's/^   Volume Name:              //p')
      
    • Finally, when you use the Boot variable, you need to put double-quotes around it to keep it from being split into multiple "words":

      echo "$Boot"
      ./startosinstall --volume "/Volumes/$Boot" --agreetolicense --rebootdelay 200
      

    BTW, shellcheck.net is good at catching common mistakes; I'd strongly recommend running your scripts through it.