Domanda

Looking for a correct and simple way to find a mountpoint in a shell script.

This working as expected

tmp=$(mktemp /tmp/my.XXXXXX)
mpoint=$(diskutil info -plist disk1s1 >"$tmp" ; /usr/libexec/PlistBuddy -c 'Print MountPoint' "$tmp")
rm -f "$tmp"

but it needs a temporary file.

While it isn't a big deal, but still would be nicer to do it without of the tempfile.

Unfortunately, the

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

doesn't works.

It is a bug of the PlistBuddy or I missed something?

Using zsh in fresh Big Sur installation.

È stato utile?

Soluzione

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)"

Altri suggerimenti

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.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a apple.stackexchange
scroll top