My goal is to create a list of partitions for each block device listed in /sys/block;
#!/bin/bash
block_devices_list=($(ls /sys/block))
partition_list=($(cat /proc/partitions | awk '{print $4}'))
unset partition_list[0]
for block_device in ${block_devices_list[@]}; do
for partition in ${partition_list[@]}; do
partitions+=($(echo $partition | grep $block_device))
done
# Right here?
unset partitions
done
Every time the outside 'for loop' completes it's cycle it ends up with an array of partitions for a particular block device. At that point I would like to transfer that data to a separate array, dynamically named after the device it belongs to (like 'partitions_sda' for example).
I have read a few questions/answers about 'dynamic' variable names, 'associative' arrays and whatnot but don't seem to be able to figure this out. Any help much appreciated.
block_devices_list=$(ls /sys/block), or preferablyblock_devices_list=$(lsblk -d)etc..block_devices_list=($(ls /sys/block))is needed to save the result as an array.lsblkoutput and some bash magic. see my answer below.block_devices_list=( /sys/block/* )avoids parsingls, which is widely (and rightly) considered bad practice; if one wants to avoid the directory-name prefixes, one can then runblock_devices_list=( "${block_devices_list[@]#/sys/block/}" )