sunnuntaina, elokuuta 23, 2009

Learning XPath

Many of the Mac OS X command line utilities have a -plist option, which makes the utility do its thing in the "plist xml" format. I had the idea that maybe it would a good idea to use it and then use some of the xml tools to extract the information I want, instead of the usual pipeline of grep, awk and sed commands.

Specifically, I wanted to find out the mount point of a file system. I know the device file of the disk partition/slice.

I could try the mount command, but if the mount point directory has whitespace characters, parsing the output of the mount command is a bit fragile.

diskutil info is an OS X specific command that gives me what I want. After learning about XPath a bit, I came up with something like this:

diskutil info -plist /dev/disk0s2 | xpath "//string[preceding-sibling::key[1]='VolumeName']"
This is not yet perfect. The volume name is surrounded by <string> and </string> but I can live with that. The final insight for the XPath expression came from this post.

P.S. This post was written in Blogger's rich text editor in the Safari browser. This was the first time it worked well enough to be useful.

1 kommentti:

jautero kirjoitti...

I'm sure there is a way to refer to contents of an element in XPath.