首页云计算 正文

blkid命令在脚本中处理多个分区

2024-12-06 4 0条评论

blkid命令在脚本中处理多个分区

发布时间:2024-10-10 15:36:31 来源:亿速云 阅读:82 作者:小樊 栏目:建站服务器

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 和文件系统类型。

文章版权及转载声明

本文作者:admin 网址:http://news.edns.com/post/179426.html 发布于 2024-12-06
文章转载或复制请以超链接形式并注明出处。

取消
微信二维码
微信二维码
支付宝二维码