Hi Everyone. Shell scripting neophyte here.
I'm trying to create a script to a mount a DMG, copy all files from the DMG, then unmount the DMG And put it in the trash. The complication is that the mounted name of the DMG has spaces and a changing numerical suffix. I have not been able to figure out how to handle both the spaces and apply a wildcard for the numerical suffix in the same variable. If I use double quotes to bypass the spaces, then the * is parsed literally. If I don't double quote, then I can't account for the spaces in the directory name of the mounted DMG volume.
I modified a script I found on Spiceworks. The original script was dealing with a known, unchanging volume name for a specific DMG and a specific application in the DMG. I'm trying to allow for variation in the ending of the mounted volume path, and to ensure that all unhidden files of all file types are copied from the DMG to my destination.
So, for example, I have this DMG for the Dolphin emulator: dolphin-master-2407-17-universal.dmg
When I mount the DMG it will mount at: /Volumes/Dolphin\ 2407-17
The 2407-17
is a number that will change unpredictably, so I wanted to use a wild card as a substitution. The DMG contains these files:
/Volumes/Dolphin\ 2407-17/Dolphin\ Updater.app
/Volumes/Dolphin\ 2407-17/Dolphin.app
Some of my DMGs will contain text files and the names will sometimes be related to the app and sometimes will just be a README.txt
or similar. So again, I wanted to use a wildcard that would just grab any file (though it would be convenient if I could limit to only visible files).
#!/bin/zsh
# Name of DMG file
dmgfile=$HOME/Downloads/dolphin-master-2407-17-universal.dmg
# Name of DMG volume
dmgvol=/Volumes/Dolphin*
# Name of files to copy
filenames=*
# Destination for copy
filedestination=/Volumes/MX500 2TB SSD 02/Games/Emulators/Dolphin/
/usr/bin/hdiutil attach "$dmgfile" -nobrowse -quiet
cp -rf "$dmgvol/$filenames" "$filedestination/$filenames"
chmod -R 777 "$filedestination/$filenames"
/bin/sleep 5
/usr/bin/hdiutil detach "$(/bin/df | /usr/bin/grep "$dmgvol" | awk '{print $1}')" -quiet
/bin/sleep 5
/bin/rm -rf /tmp/"$dmgfile"
xattr -d com.apple.quarantine "$destination/$filenames"
exit 0
I've been trying to figure this out through reading through various forums, but I'm getting a bit lost in the nuances of expansions, substitutions, parameters and variables. Any suggestions or solutions would be much appreciated.