1

I am trying to automate (using bash script)creation of JHFS+ formatted,DMG file on a macOS. The script should be able to intake user supplied:

  • size of the DMG in GB's
  • destination of the DMG
  • type of filesystem (HFS+,JHFS+,APFS,FAT32,ExFAT or UDF)
  • volume name
  • the name of the DMG to be created.

I used the following:

date
read -p "Enter the size of the DMG:   " size        
read -e -p "Enter the destination of the DMG:   " dest  
read -p "Enter the filesystem (HFS+,JHFS+,APFS,FAT32,ExFAT or UDF)  :" fs
read -p "Enter the Volume name :" volname
read -p "Enter the name of the DMG:" name

hdiutil create -fs {"$fs"} -size "$size" -volname "{$volname}" "{$dest\/$name}"
exit

Issue is when the script is executed,with size mentioned as "1g" (1GB),I get the following error:

hdiutil: create failed - Invalid argument

Suggestions to improve the script are welcome.Thanks in advance :)

1 Answer 1

1

The curly brackets { and } around the variables are wrong and the escaped slash \/ doesn't work.

I changed $dest and $name to a combined $dest and added default values. For the size I added a little hint for the most common sizes.

#!/bin/bash

defaults=( 1g HFS+ "my volume" ~/Desktop/myvolume.dmg )

read -ep "Enter the size (??m|??g|??t) [${defaults[0]}] " size 
read -ep "Enter the filesystem (HFS+, JHFS+, APFS, FAT32, ExFAT, UDF) [${defaults[1]}] " fs
read -ep "Enter the volume name [${defaults[2]}] " volname
read -ep "Enter the image destination [${defaults[3]}] " dest

hdiutil create -size "${size:-${defaults[0]}}" -fs "${fs:-${defaults[1]}}" -volname "${volname:-${defaults[2]}}" "${dest:-${defaults[3]}}"
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.