Originally Posted By: Virtual1
DSCL is used to tell if the user is an admin, not root. if they're not an admin (which root incidently is) they can't use sudo so there's no point in asking for their password.

Okay, but the 'id' way is smaller and probably (a skosh) quicker. wink

--

Going for that gold star now (well, a silver at least):

Notes:
  1. Even though my script includes a $tSize variable [initially reserving 20 gigs and then calculating the full total (based on all uncommented partition data)], it never actually makes use of that info. [see 4.]

  2. I have skipped the whole authorization business, but it can be pasted back in easily enough. (i'm not sure if sudo is even needed, since Disk Utility in the GUI doesn't usually ask for a password unless the operator is not an admin). My test runs all worked sans sudo anyway. [see 4.]

  3. Also skipped various sanity checks, as well as the whole disk analysis/selection part (instead offering only a peek at df to let the user glimpse the device numbers). [see 4.]

  4. The reason for all of the above is simply that my focus was to find a way of getting diskutil to accept some sort of data input when the volume names contain spaces.

    I too initially gave up on the "single variable" concept after many failed experiments... but eventually concluded that it would indeed be needed for any practical (useful) script solution. [one alternative would entail a 12-stage case statement, which would be lengthy, ugly, and less flexible in the end.]

    Luckily, i stumbled onto a mechanism to get the "single variable" to work. Quite simply: put hard quotes around soft quotes, and have Bash properly pass the junk to diskutil via its builtin eval command (which exists in the Bourne sh too). It's quite possible that eval might allow some other quoting methods to work, but at this point i'm placing the whole matter into the 'mission accomplished' folder.

Code:
#!/bin/bash -
# psd  :::  Partition Service Disk
# flexible script to facilitate disk partitioning
#(c)EF/-HI-2011.Jun.07 [rev:2011.Jun.10]

IFS=$' \t\n'
declare -x PATH=/bin:/sbin:/usr/bin:/usr/sbin
declare -i pNum=0 dNum=666 # <--hopefully imaginary
PRG=`basename "$0"`

# variable data may be edited or commented out entirely...
# >========================================>
name1='"Service Lion"'		size1=17
name2='"Service Snow"'		size2=15
name3='"Service Leopard"'	size3=23
name4='"Service Tiger"'		size4=12
name5='"Mac OS 10.3.4"'		size5=1
name6='"Mac OS 10.3.4 Disc 2"'	size6=1
name7='"Mac OS 10.4.6"'		size7=4
name8='"Mac OS 10.4.7"'		size8=6
name9='"Mac OS 10.5.6"'		size9=8
name10='"Mac OS 10.6.3"'	size10=9
name11='"Mac OS 10.7.0"'	size11=9
name12='"Service Data"'		size12=R
# <========================================<
# ...BUT, the final partition MUST be given a size of R

# reserve 20 gigs for the final (data) partition:
tSize=20
# count (and list) partitions:
for n in ${!name*}; do ((pNum+=1)); echo "${!n}"; done
echo
echo "$pNum partitions desired."
# calculate total size:
for s in ${!size*}; do ((tSize+=$s)); done # note: Bash math ignores the 'R'
echo "$tSize gigabytes needed."
echo

df -l -h
printf '\nenter the integer of the /dev/disk_ to partition '
read dNum
echo

c=
for i in {1..12}
do
	j=name$i k=size$i
	if [[ -n ${!j} ]]
	then
		[ ${!k} = R ] && g= || g=G
		c="$c JHFS+ ${!j} ${!k}$g"
	fi
done

printf 'This script (%s) is ready to execute the following command:\n\n' "$PRG"
echo diskutil partitionDisk /dev/disk$dNum $pNum APM $c

doScript=n
printf '\nShall we allow this script to erase /dev/disk%d ? (y/n [n]) ' $dNum
read doScript
echo
if [[ $doScript != y ]]
then
	echo "$PRG: task aborted."
	exit 1
else
	eval diskutil partitionDisk /dev/disk$dNum $pNum APM $c
	exit $?
fi
 


-HI-

Last edited by Hal Itosis; 06/10/11 04:24 AM. Reason: oops, i left an 'M' in there from my Flash disk tests. [and subsequent minor tweak]