The problem is the output of `hdiutil` includes trailing spaces and tabs:
$ hdiutil attach -nomount ram://2097152 | od -tx1z
0000000 2f 64 65 76 2f 64 69 73 6b 36 20 20 20 20 20 20 >/dev/disk6 <
0000020 20 20 20 20 09 20 20 20 20 20 20 20 20 20 20 20 > . <
0000040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 > <
0000060 20 20 20 20 09 0a > ..<
0000066
That isn't an issue in zsh due to the way commands are split more than once
on $IFS chars. You can fix the output by piping it through `string trim`.
For example,
function ramdisk
# calculate a size in GB for a new ramdisk
set -l size (math "$argv[1] * 2 * 1024^2")
# ensure any existing ramdisk is unceremoniously dumped
diskutil eject /Volumes/ramdisk >/dev/null 2>&1
# create an unformatted ramdisk - works
set -l ramdiskpath (hdiutil attach -nomount ram://$size | string trim)
# format the disk using the path - fails
diskutil erasevolume 'HFS+' ramdisk $ramdiskpath
end
Notice that I'm also using `set -l` to create local vars so as not to
pollute the global namespace if those vars happen to be defined globally.
I'm also using the clearer `2 * 1024^2` than the literal value for 2MiB.
I'd also recommend sanity checking that $argv[1] is set to a sane value.
On Wed, Nov 30, 2016 at 5:14 AM, Dave Cottlehuber <[email protected]> wrote:
> Hi fisherfolk,
>
> This is a copy-pasta function from my previous zsh setup, on OSX., to
> create a ramdisk of X Gb. Unfortunately the final command, diskutil
> erasevolume ... fails unless run manually. I've tried sleep 5,
> sprinkling quotes, wrapping it in (...) but nada. Any idea why?
>
> fish 2.3.1 via homebrew on OSX
>
> ```fish
> function ramdisk
> # calculate a size in GB for a new ramdisk
> set size (math "$argv[1] * 2097152")
> # ensure any existing ramdisk is unceremoniously dumped
> diskutil eject /Volumes/ramdisk >/dev/null 2>&1
> # create an unformatted ramdisk - works
> set ramdiskpath (hdiutil attach -nomount ram://$size)
> # format the disk using the path - fails
> diskutil erasevolume HFS+ ramdisk $ramdiskpath
> end
> ```
>
> $ ramdisk 12
>
> Unable to find disk for /dev/disk1
>
> -- sadpanda --
>
> $ diskutil list
>
> /dev/disk0 (internal, physical):
> #: TYPE NAME SIZE
> IDENTIFIER
> 0: GUID_partition_scheme *3.0 TB
> disk0
> 1: EFI EFI 209.7 MB
> disk0s1
> 2: Apple_HFS continuity 299.5 GB
> disk0s2
> 3: Apple_Boot Recovery HD 650.0 MB
> disk0s3
> 4: 516E7CBA-6ECF-11D6-8FF8-00022D09712B 2.7 TB
> disk0s4
>
> /dev/disk1 (disk image):
> #: TYPE NAME SIZE
> IDENTIFIER
> 0: +12.9 GB
> disk1
>
> -- do it manually --
>
> $ diskutil erasevolume HFS+ ramdisk "/dev/disk1"
> Started erase on disk1
> Unmounting disk
> Erasing
> Initialized /dev/rdisk1 as a 12 GB case-insensitive HFS Plus volume
> Mounting disk
> Finished erase on disk1 ramdisk
>
> A+
> Dave
>
> ------------------------------------------------------------
> ------------------
> _______________________________________________
> Fish-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/fish-users
>
--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
------------------------------------------------------------------------------
_______________________________________________
Fish-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/fish-users