You can skip the 2 header lines with tail --lines=+3
, to avoid capturing the column titles and dash line separator header.
This is how a virsh list --all
look like:
2 header lines to skip with tail --lines=+3
:
Id Name State
----------------------------------------------------
Data to parse:
1 openbsd62 running
2 freebsd11-nixcraft running
3 fedora28-nixcraft running
After it skip the header line of the domains list, the script below iterates over each domain in a while read
loop witch receives its data from the virsh list --all | tail --lines=+3 | awk '{print $2}'
Then inside the while loop, it maps the output of virsh domblklist "$domain" --details | tail --lines=+3 | awk '{print $4}'
into the temporary file-mapped array MAPFILE
;
and add the MAPFILE
array entries to my_array
After execution, my_array
contains all block devices from all domains.
#!/usr/bin/env bash
declare -a my_array=() # array of all block devices
# Iterate over domains that are read from the virsh list
while IFS= read -r domain; do
mapfile < <( # capture devices list of domain into MAPFILE
# Get block devices list of domain
virsh domblklist "$domain" --details |
# Start line 3 (skip 2 header lines)
tail --lines=+3 |
# Get field 4 s value
awk '{print $4}'
)
my_array+=( "${MAPFILE[@]}" ) # Add the block devices paths list to my_array
done < <( # Inject list of domains to the while read loop
# List all domains
virsh list --all --name
)