Finding the mountpoint of an device in shell scripts

Solution 1:

Use a here string to pass the output of diskutil to standard in.

/usr/libexec/PlistBuddy -c "Print :MountPoint" /dev/stdin <<< "$(diskutil info -plist disk1s1)"

Solution 2:

It's a limitation of PlistBuddy that it can only read from a file, not from a pipe. Contructs like … | /usr/libexec/PlistBuddy /dev/stdin and /usr/libexec/PlistBuddy /dev/stdin <(…) use a pipe to plug the output of into the input of PlistBuddy. You need to use a temporary file. But you can have the shell do it for you. Here-documents and here-strings use a temporary file, as does zsh's =(…) process substitution:

/usr/libexec/PlistBuddy -c 'Print MountPoint' <<EOF
$(diskutil info -plist disk1s1)
EOF
/usr/libexec/PlistBuddy -c 'Print MountPoint' <<<$(diskutil info -plist disk1s1)
/usr/libexec/PlistBuddy -c 'Print MountPoint' =(diskutil info -plist disk1s1)

Depending on what you're doing, you may be able to get mount point information from df instead.

df -P |awk '$1 == "/dev/disk1s1" {print $6}'

(Note that this simple version does not handle mount point paths containing spaces correctly.)