Here's the final working script. I have commented out most of the partitions to make your testing go quicker:

Code:

#!/bin/bash

vers="2011.06.13.B"
tempfile=/var/tmp/ptemp

# build list of partitions to create
pl=""
pl="${pl} Service_Lion=17"
#pl="${pl} Service_Snow=15"
#pl="${pl} Service_Leopard=23"
#pl="${pl} Service_Tiger=12"
#pl="${pl} Mac_OS_10.3.4=1"
#pl="${pl} Mac_OS_10.3.4_Disc_2=1"
#pl="${pl} Mac_OS_10.4.6=4"
#pl="${pl} Mac_OS_10.4.7=6"
#pl="${pl} Mac_OS_10.5.6=8"
#pl="${pl} Mac_OS_10.6.3=9"
pl="${pl} Mac_OS_10.7.0=9"
#pl="${pl} Service_Data"  # Service Data will take the remainder
pl=${pl:1}


# count partitions
plc=$(echo "$pl" | sed 's/[^ ]//g')
plc=${#plc}
((plc++))  #  add one for diff in spaces vs entries
((plc++))  #  add one for Data


# calculate space required (in GB)
nsize=20  # set aside at least this many GB for Service data
p="$pl "
while [ -n "$p" ] ; do
  pp=${p%% *}
  p=${p#* }
  pz=${pp#*=}
  ((nsize+=pz))
done


# fyi
clear
echo
echo "PHR partitioner"
echo
echo "This script will FORMAT a hard drive.  All information on"
echo "the hard drive will be lost!  You will need a capacity of"
echo "at least ${nsize} GB."
echo
echo


# authenticate
if [ $EUID != 0 ] ; then
  admins=" $(dscl . -read /Groups/admin | grep GroupMembership | cut -c 23-999) "
  if [ -z "$(echo "$admins" | grep " ${USER} ")" ] ; then
    echo "You must be logged in as an administrator to run this installer."
    exit
  fi
  sudo -k
  echo "Enter your administrator password to continue:"
  sudo -v
  if [ $? != 0 ] ; then
    echo "Cannot continue without password.  If your password is blank,"
    echo "you will need to change it to something non blank."
    exit
  fi
  sudo "$0"
  exit
fi
echo
echo "Authenticated as an administrator."
echo
echo


# build list of eligible destinations, into array
echo "Checking attached volumes..."
echo
diskutil list | grep "^/dev/disk" > $tempfile
dc=0
options=""
while read d ; do
  echo -n "Found device $d... "
  if [ "$d" == "/dev/disk0" ] ; then
    echo "(booted)"
    continue
  elif [ -z "$(diskutil info $d | grep "Ejectable" | grep -i "yes")" ] ; then
    echo "(not ejectable)"
    continue
  elif [ -z "$(diskutil info $d | grep " Read" | grep -i "no")" ] ; then
    echo "(read-only)"
    continue
  elif [ -n "$(diskutil info $d | grep " Protocol" | grep -i "Disk Image")" ] ; then
    echo "(disk image)"
    continue
  else
    ds=$(diskutil info $d | grep "^ \+Total Size" | cut -d "(" -f 2 | cut -d " " -f 1)
    ((ds=ds/1024/1024/1024))  #  capacity in GB
    if [ $ds -lt $nsize ] ; then
      echo "($ds GB is too small to install onto)"
      continue
    fi
  fi
  echo "(eligible)"
  devs[dc]=$d
  ((dc++))
  options="$options $d"
done < $tempfile
rm $tempfile
echo
echo


# verify at least one eligible device
if [ $dc == 0 ] ; then
  echo "No eligible volumes found."
  echo
  echo "Conncet a hard drive and rerun script to partition for PHR"
  echo
  exit
fi
options=${options:1}


# list partitions on each eligible device
for ((di=0;di<dc;di++)) ; do
  d=${devs[di]}
  echo "Listing partitions on device $d..."
  diskutil list $d | grep " ${d#/dev/}s[0-9]*$"
  echo
done
echo
echo


# select destination device
echo "Select device to repartition:"
select d in $options ; do
  if [ -n "$d" ] ; then
    echo
    echo "Device selected: $d"
    break
  fi
done
echo
echo

# get device size
ds=$(diskutil info $d | grep "^ \+Total Size" | cut -d "(" -f 2 | cut -d " " -f 1)
((ds=ds/1024/1024/1024))  #  convert bytes to gb
echo "$nsize GB is required to install PHR.  The selected device is $ds GB in capacity."
echo
echo

# verify adequate capacity
# unnecessary now since only volumes with enough space are selectable above
#if [ $ds -lt $nsize ] ; then
#  echo
#  echo "Sorry, we require at least a $nsize GB drive"
#  echo
#  exit
#fi


# last chance to chicken out
echo "The following partitions will be ERASED:"
diskutil list $d | grep " ${d#/dev/}s[0-9]*$"
echo
echo -n $'\a\a\a'
while true ; do
  read -p "Type \"FORMAT\" to erase and reformat $d: " c
  if [[ ("$c" == "FORMAT") || ("$c" == "format") ]] ; then
    break
  fi
  echo $'\a'
done
echo
echo


# unmount volumes before attempting repartition
clear
echo
echo "Unmounting volumes on $d..."
diskutil list $d | grep " ${d#/dev/}s[0-9]*$" | while read p ; do
 pd="/dev/${p##* }"
  echo -n "  unmounting $pd... "
  hdiutil unmount $pd 2> /dev/null > /dev/null
  if [ $? == 0 ] ; then
    echo "done."
  else
    echo "unable to unmount.  Be sure all files on device are closed."
    exit
  fi
done
echo
echo


# since diskutil cannot parse volume names with spaces in them when
# passed in as individual shell variables, it's necessary to create a
# shell script file with the partitioning command in it


# create partitioning script
touch "$tempfile"
chmod +x "$tempfile"
echo "#!/bin/bash" >> "$tempfile"
c="diskutil partitionDisk $d $plc APM"
for p in $pl ; do
  pv=${p%=*}
  pg=${p#*=}
  pv="'"$(echo "$pv" | sed "s/_/ /g")"'"
  c="$c JHFS+ ${pv} ${pg}G"
done
c="$c JHFS+ 'Service Data' R"  #  Data volume takes remainder
echo -n "$c" >> "$tempfile"
echo ' 1>&2' >> "$tempfile"  # diskutil output sent to stderror to display
echo 'echo "$?"' >> "$tempfile"  # process result echoed to shell var "rc"


# run partition script
echo "Creating partition map.  This will take a few minutes."
echo
echo "running command: $c"
echo
#cat "$tempfile"
rc=$("$tempfile")
rm "$tempfile"
echo
echo


# check result
echo
if [ "$rc" == 0 ] ; then
  echo 'Success!'
else
  echo "Format failed, diskutil returned error code $rc"
fi
echo
exit




I work for the Department of Redundancy Department