I'm writing a bash script and struggling to find a way to create an associative array that holds a key and one attribute to each mounted external hard drive plugged in via USB. My example used here is an associative array, but it does not have to be. Though I feel it makes the most sense. And yes I am using bash 4 for the associative array support.
> /bin/bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0)
Currently I'm playing around with diskutil list
and system_profiler SPUSBDataType
as subshells in a for
loop. Or just parsing a bunch of the output through pipes with a mixture of sed
, grep
and awk
commands. So far no avail. It should be noted that these apple commands output other things I want to discard such as bluetooth devices and my local hard drive. See below for details.
My desired array would look something like this: myArray=([Name1]=/dev/disk3s2 [Name2]=/dev/disk4s2)
. I really just need the name and it's mount point. The format of it is flexible. I can parse it later if in a different format. I just need to be able to do a simple for i in "!${myArray[@]}; do echo "Name = ${i}"; echo "mount point = ${myArray[$i]}"; done
or something similar.
Note: at the end of the day, its will prompt the user something like "You have N number of external hard drives mounted, please select which one you would like to perform the back up to"
As requested here is example output of the main two commands (diskutil list
and system_profiler SPUSBDataType
) I have been using in order to try and achieve my goal. Though it should be noted, I don't care if there are other ways to achieve the same desired goal. If you know of other useful commands, please suggest.
> diskutil list
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *500.3 GB disk0
1: EFI EFI 209.7 MB disk0s1
2: Apple_CoreStorage Macintosh HD 499.4 GB disk0s2
3: Apple_Boot Recovery HD 650.0 MB disk0s3
/dev/disk1 (internal, virtual):
#: TYPE NAME SIZE IDENTIFIER
0: Apple_HFS Macintosh HD +499.1 GB disk1
Logical Volume on disk0s2
A0B29294-0031-40CB-8894-6F56545CCF3C
Unencrypted
/dev/disk3 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *2.0 TB disk3
1: EFI EFI 209.7 MB disk3s1
2: Apple_HFS Storage 2.0 TB disk3s2
and
> system_profiler SPUSBDataType
USB:
USB 3.0 Bus:
Host Controller Driver: AppleUSBXHCIWPT
PCI Device ID: 0x9cb1
PCI Revision ID: 0x0003
PCI Vendor ID: 0x8086
BUP Slim SL:
Product ID: 0xab26
Vendor ID: 0x0bc2 (Seagate LLC)
Version: 1.08
Serial Number: NA96KBJB
Speed: Up to 5 Gb/sec
Manufacturer: Seagate
Location ID: 0x14500000 / 3
Current Available (mA): 900
Current Required (mA): 896
Extra Operating Current (mA): 396
Media:
BUP Slim SL:
Capacity: 2 TB (2,000,398,933,504 bytes)
Removable Media: No
BSD Name: disk3
Logical Unit: 0
Partition Map Type: GPT (GUID Partition Table)
USB Interface: 0
Volumes:
EFI:
Capacity: 209.7 MB (209,715,200 bytes)
File System: MS-DOS FAT32
BSD Name: disk3s1
Content: EFI
Volume UUID: 0E239BC6-F960-3107-89CF-1C97F78BB46B
Storage:
Capacity: 2 TB (2,000,054,960,128 bytes)
Available: 1.3 TB (1,298,787,659,776 bytes)
Writable: Yes
File System: Journaled HFS+
BSD Name: disk3s2
Mount Point: /Volumes/Storage
Content: Apple_HFS
Volume UUID: E9786DAE-78A7-31B7-B939-8FCCE3C147AB
Bluetooth USB Host Controller:
Product ID: 0x8290
Vendor ID: 0x05ac (Apple Inc.)
Version: 1.46
Speed: Up to 12 Mb/sec
Manufacturer: Broadcom Corp.
Location ID: 0x14300000 / 2
Current Available (mA): 500
Current Required (mA): 0
Extra Operating Current (mA): 0
Built-In: Yes
diskutil list | tail -n +15
will give me basically everything after my local hard drive.
Another thought I had was I could do some sort of for usb in $(system_profiler SPUSBDataType)
statement. But I can't figure out the right subshell command to pipe to sed
,grep
,awk
and other tools to parse by blank lines or indentation. More than likely I will not be able to cleanly implement it in one swipe like that.