1

I've written a little script that uses the hdiutil in OSX but I'm having an issue when passing a path to the output variable where the path contains spaces. The path is generated simply by the user dragging the path folder onto the terminal window and then reading that to a variable. Basically I have for the path:

echo "Drag Destination Folder to Window or Leave Blank to Create on Desktop:"
read createDest
createDest=$(echo "$createDest" | sed 's/ /\\ /g')

I then prompt for the desired name with

echo "Enter Name for Image (also used for Volume):"
read varName
varName=$(sed -e 's/[^A-Za-z0-9._-]/_/g' <<< $varName) #remove illegal chars

Combine them with path=$(echo ${createDest}/${varName})

And finally generate the file with

Echo -n $varPass | hdiutil create -encryption -stdinpass -type SPARSEBUNDLE -size ${varSize}G -fs HFS+J -volname $varName $path

This all works fine as long as there are no spaces in the path but as soon as there are I get an error from hdiutil stating:

hdiutil: create: Only one image can be created at a time.

If I type the pass in manually its fine so I'm a bit confused as to where my formatting has gone wrong.

Any help would be very much appreciated.

Cheers

Chris

2
  • You do not want to manually escape the spaces in your path; the backslashes you are adding are literal data, not protecting spaces in the value of createDest. Quoting the expansion is sufficient. Commented Oct 6, 2018 at 16:54
  • Perfect. That got it. Thanks chepner. Commented Oct 6, 2018 at 18:12

2 Answers 2

1

All you need to do is quote the parameter expansions; that is sufficient to protect the whitespace. Adding backslashes manually simply creates a value that does not represent an existing path.

echo "Drag Destination Folder to Window or Leave Blank to Create on Desktop:"
read createDest

echo "Enter Name for Image (also used for Volume):"
read varName
varName=${varName//[^[:alnum:]._-]/_}  # more efficient than running sed

printf '%s' "$varPass" | hdiutil create -encryption -stdinpass -type SPARSEBUNDLE -size "${varSize}G" -fs HFS+J -volname "$varName" "$path"
Sign up to request clarification or add additional context in comments.

Comments

1

Quote all variables everywhere:

echo -n "$varPass" | hdiutil create -encryption -stdinpass -type SPARSEBUNDLE -size "${varSize}G" -fs HFS+J -volname "$varName" "$path"

10 Comments

Hmm. Tried that but it just throws an error saying hdiutil: create failed - No such file or directory
Good! That means that we solved your problem and now you have another! :-) Check if the file or directory the program is expecting exists and is accessible (rw permissions).
Definitely does exist as I dragged it to the terminal to input the path and seems to have rw permissions. It works fine still for paths without spaces. Just any folder I create with spaces are broken.
What is the result of echo "$path" and ls -d $(dirname "$path")?
I get Users/me/Desktop/a\ test/1 ls: /Users/me/Desktop/a\: No such file or directory ls: test: No such file or directory
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.