So what I have here is part of my script that is used to read the pcie lanes on the boards to help understand what NVMe's are up and running without having to manually check each drive. This works for the most part, however, my knowledge in bash has failed me again and I'm not really sure what I should using in place of:
elif [[ ! "10000:01:00.0" =~ "${V2Lnvmearray[$key]}" ]]
As this doesn't just if a drive is missing but is also true when for the other look ups that dont match when a drive is present. This results in either 8 counts of "no drive detected" per slot or 7 counts of "No drive detected" and a positive count. Thanks for any input.
#!/bin/bash
V2Lnvme0n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme0n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
V2Lnvme1n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme1n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
V2Lnvme2n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme2n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
V2Lnvme3n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme3n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
V2Lnvme4n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme4n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
V2Lnvme5n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme5n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
V2Lnvme6n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme6n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
V2Lnvme7n1=$( readlink -f /sys/dev/block/$(ls -l /dev/nvme7n1 |awk -F'[, ]+' '{print $5":"$6}') |grep -Eo '1000+[0-4]+[:]+[0]+[1-4]+[:]+[0]+[.]+[0]+' )
declare -A V2Lnvmearray=( [nvme0n1]=$V2Lnvme0n1 [nvme1n1]=$V2Lnvme1n1 [nvme2n1]=$V2Lnvme2n1 [nvme3n1]=$V2Lnvme3n1 [nvme4n1]=$V2Lnvme4n1 [nvme5n1]=$V2Lnvme5n1 [nvme6n1]=$V2Lnvme6n1 [nvme7n1]=$V2Lnvme7n1 )
#V2Large Machines setup | get the PCIe address into the array
for key in "${!V2Lnvmearray[@]}"; do
echo "$key ${V2Lnvmearray[$key]}"; done # displays the keys and values of the array
for key in "${!V2Lnvmearray[@]}"; do
if [[ "10000:01:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 0"
elif [[ ! "10000:01:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 0"
fi
if [[ "10000:02:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 1"
elif [[ ! "10000:02:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 1"
fi
if [[ "10002:01:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 2"
elif [[ ! "10002:01:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 2"
fi
if [[ "10002:02:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 3"
elif [[ ! "10002:02:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 3"
fi
if [[ "10001:01:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 4"
elif [[ ! "10001:01:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 4"
fi
if [[ "10001:02:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 5"
elif [[ ! "10001:02:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 5"
fi
if [[ "10001:03:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 6"
elif [[ ! "10001:03:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 6"
fi
if [[ "10001:04:00.0" = "${V2Lnvmearray[$key]}" ]]; then
echo "$key is in slot 7"
elif [[ ! "10001:04:00.0" =~ "${V2Lnvmearray[$key]}" ]]; then
echo "No drive detected in slot 7"
fi
done
elif
branches are exactly the same as the ones in the correspondingif
branches, just negated. So you could just useelse
instead. That very first array initialization just screams for a loop,for nvm in nvme{0..7}n1 ; do V2Lnvmearray[$nvm]=$(... /dev/$nvm ...); done
. So does the inside of the main loop too,for x in 10000:01:00.0 10000:02:00.0 ...
– ilkkachu Oct 18 '21 at 10:55