Parse hdiutil -plist data for specific info

I am trying to get the mounted size of a compressed .dmg. I can use hdiutil imageinfo -plist $imagename to get a lot of info, including what I need. The problem is that I have no idea how to parse that single piece of info into a variable (or in this case, array element). Here is what I have so far

#!/bin/bash
#first part of script which generates the array below
for i in ${DMGLIST[@]} #This array contains a list of .dmg files
	do
	hdiutil imageinfo -plist $i | #some way to parse stdout for the info and save it to a variable
	done

The info I need is in the middle of the output

	<key>Total Bytes</key>
		<integer>73400320</integer>

And I have no idea how to go about parsing through it since the string is broken up by the </key> and <integer> things. I can think of some complex, inelegant ways, but I'm sure someone here has an idea for how to do it better! Thanks!

Try this (Array DMGSIZE will contain size of each .dmg file in DMGLIST):

#!/bin/bash
#first part of script which generates the array below
pos=0
for i in ${DMGLIST[@]} #This array contains a list of .dmg files
do
    DMGSIZE[pos++]=$(hdiutil imageinfo -plist $1 | awk -F"[<>]" 'a{print $3; exit}$2=="key"&&$3=="Total Bytes"{a=1}')
done

Though it may be better to generate DMGSIZE at the same time you are generating DMGLIST, ie in the bit of the script not shown.

1 Like

Perfect. I had to change the 1 to i, but everything else works exactly. Now to look up how this works for future reference. Thanks!