Last active
January 25, 2024 22:16
-
-
Save putnamhill/5aeeeeb273eb57f4ce7b39b49a28a376 to your computer and use it in GitHub Desktop.
Steps to attach a dmg disk image using hdiutil while capturing the mount point and dev entry for detaching when done
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dmg_path="$1" | |
# use process redirection to capture the mount point and dev entry | |
IFS=$'\n' read -rd '\n' mount_point dev_entry < <( | |
# mount the diskimage (leave out -readonly if making changes to the file system) | |
hdiutil attach -readonly -plist "$dmg_path" | \ | |
# convert output plist to json | |
plutil -convert json - -o - | \ | |
# extract mount point and dev entry | |
jq --raw-output ' | |
.[] | .[] | | |
select(."volume-kind" == "hfs") | | |
."mount-point" + "\n" + ."dev-entry" | |
' | |
) | |
# work with the file system | |
echo "ls $mount_point:" | |
ls "$mount_point" | |
# unmount the disk image (use the -force option if necessary) | |
hdiutil detach "$dev_entry" |
This script is good BUT the "mount_point" does not take into account anything that can contain a space.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for this!!!
This was very helpful, since jq is not installed by default on a Mac, I made some modifications that also simplifies the process a little:
Also: Please notice that this in this version you should omit the .dmg on the parameter for the script