blkid
命令用于显示块设备的 UUID 和文件系统类型
#!/bin/bash
# 存储设备 UUID 和文件系统的列表
declare -A device_info
# 获取 /etc/fstab 文件中的设备信息
while IFS= read -r line; do
# 检查行是否包含 /dev/sdX 或 /dev/nvmeXnY 的格式
if [[ $line =~ ^/dev/sd[a-z]+(/[a-z]+)?$ ]] || [[ $line =~ ^/dev/nvme[a-z]+n[a-z]+(/[a-z]+)?$ ]]; then
# 获取设备 UUID 和文件系统类型
uuid=$(echo "$line" | cut -d ' ' -f 5 | cut -d '"' -f 2)
filesystem=$(echo "$line" | cut -d ' ' -f 2 | cut -d '=' -f 2)
# 将设备 UUID 和文件系统类型存储到关联数组中
device_info["$uuid"]=$filesystem
fi
done < /etc/fstab
# 遍历关联数组并输出设备 UUID 和文件系统类型
for uuid in "${!device_info[@]}"; do
echo "Device UUID: $uuid, Filesystem Type: ${device_info[$uuid]}"
done
将此脚本保存为 list_partitions.sh
,然后在终端中运行 chmod +x list_partitions.sh
以使其可执行。接下来,通过运行 ./list_partitions.sh
来执行脚本。这将输出 /etc/fstab
文件中列出的所有块设备的 UUID 和文件系统类型。