function newline_to_space() { # Replaces all newlines with spaces # $1: name of variable local -n ptr=$1 # Replace newlines with spaces # See bash string substitution: https://gist.github.com/JPvRiel/b279524d3e56229a896477cb8082a72b # echo "replacing newlines in str: -->$ptr<--" ptr="${ptr//$'\n'/' '}" # echo "new str is: -->$ptr<--" } function newline_separated_to_array() { # Watch out for tailing newlines as these will get an empty array entry at the end. # $1 and $2 can be equal (if the result shall be written to the input variable) # # $1: name of variable with newline separated list # $2: name of array to store values into local -n ptr=$1 local -n ptr2=$2 # ptr newline separated list. # Store this as array ptr2. readarray -t ptr2 <<<"$ptr" } function space_separated_to_array() { # $1: name of variable with space separated list # $2: name of array to store values into local -n ptr=$1 local -n ptr2=$2 # ptr space separated list. # Store this as array ptr2. # Without newline at last array element: https://unix.stackexchange.com/a/519917/315162 readarray -d " " -t ptr2 < <(printf '%s' "$ptr") } function get_block_devices() { # return: the following variables: # BLOCK_DEVICES (array with one entry for each block device) # Get list of devices, one per line BLOCK_DEVICES=$(lsblk -dplnx size -o name | grep -Ev "boot|rpmb|loop" | tac) newline_separated_to_array BLOCK_DEVICES BLOCK_DEVICES } function get_block_devices_with_size() { # return: the following variables: # BLOCK_DEVICE_SIZES (array with two entries for each block device: device path and device size) # Get list of devices and their sizes, one pair per line # Use sed to remove multiple white spaces: sed 's|\s\s*| |' BLOCK_DEVICE_SIZES="$(lsblk -dplnx size -o name,size | grep -Ev "boot|rpmb|loop" | sed 's|\s\s*| |' | tac)" newline_to_space BLOCK_DEVICE_SIZES space_separated_to_array BLOCK_DEVICE_SIZES BLOCK_DEVICE_SIZES # # DEBUG # for i in "${BLOCK_DEVICE_SIZES[@]}"; do # echo "<$i>" # done # exit }