一些 Bash 有助于我的脚本中的 elif 逻辑

一些 Bash 有助于我的脚本中的 elif 逻辑

因此,我这里的脚本的一部分用于读取板上的 PCIe 通道,以帮助了解 NVMe 的启动和运行情况,而无需手动检查每个驱动器。这在大多数情况下都有效,但是,我对 bash 的了解再次让我失望,我不确定我应该使用什么来代替:

    elif [[ ! "10000:01:00.0" =~ "${V2Lnvmearray[$key]}" ]]

因为这不仅适用于驱动器缺失的情况,还适用于驱动器存在时其他查找不匹配的情况。这会导致每个插槽有 8 个“未检测到驱动器”计数或 7 个“未检测到驱动器”计数和一个正计数。感谢您的任何意见。

#!/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

答案1

为什么还要这样做?不仅是ls解析一个糟糕想法的输出,您正在查找可能已安装或什至未安装的 NVME 设备的详细信息。 stat和/或find是比ls -l.

从那里获取您需要的东西不是更简单吗find /sys/devices/pci* -name 'nvme[0-9]*n1'?这将为您提供完整的 /sys/devices 路径仅有的系统中实际安装的 nvme 驱动器。

如果你想把它放在一个数组中:

$ readarray -d '' -t nvme  < <(
    find /sys/devices/pci*/ -name 'nvme[0-9]*n1' -print0)

$ declare -p nvme
declare -a nvme=(
 [0]="/sys/devices/pci0000:40/0000:40:01.2/0000:42:00.0/nvme/nvme1"
 [1]="/sys/devices/pci0000:40/0000:40:01.1/0000:41:00.0/nvme/nvme0")

(在输出中添加换行declare -p以使其更具可读性)

如果您还希望将其放在关联数组(也称为哈希)中,并按基本名称索引:

$ readarray -d '' -t nvme  < <(
    find /sys/devices/pci*/  -name 'nvme[0-9]*n1' -print0)

$ for d in "${nvme[@]}" ; do
   nvme_hash["$(basename "$d")"="$d"
done

$ declare -p nvme_hash
declare -A nvme_hash=(
 [nvme1n1]="/sys/devices/pci0000:40/0000:40:01.2/0000:42:00.0/nvme/nvme1/nvme1n1"
 [nvme0n1]="/sys/devices/pci0000:40/0000:40:01.1/0000:41:00.0/nvme/nvme0/nvme0n1" )

或者,如果您只想要该路径的第五个元素:

$ for d in "${nvme[@]}" ; do
  nvme_hash["$(basename "$d")"]="$(printf "$d" | awk -F/ '{print $5}')"
done

$ declare -p nvme_hash
declare -A nvme_hash=([nvme1n1]="0000:40:01.2" [nvme0n1]="0000:40:01.1" )

这不会直接告诉您驱动器是否“丢失”,但会为您提供所有已安装 nvme 驱动器的完整列表 - 您可以使用它来推断是否有驱动器丢失。如果您期望有 8 个 NVME 设备,但散列只有 7 个元素,则缺少一个元素。或者,如果您希望安装 nvme[0-7]n1,但缺少 3 和 5,则可以迭代哈希的键来发现这一点。

通过该/sys/devices/...路径,您可以访问有关 nvme 驱动器的完整信息。

简而言之,您正在向后执行此操作,从硬编码的列表开始可能存在而不是发现什么实际上那里。

相关内容