将Linux文件系统类型分配给变量

问题描述 投票:0回答:1

我正在尝试将Linux文件系统类型分配给变量。我的命令输出df -Th | grep "^/dev"如下:

/dev/mapper/rhel-root xfs        50G   11G   40G  22% /
/dev/mapper/rhel-home xfs        53G  569M   53G   2% /home
/dev/sdb1             xfs      1014M  192M  823M  19% /boot
/dev/sdc1             vfat      7.6G  7.4G  125M  99% /run/media/root/RHEL-8-1-0-

我的解决方案是将其发送到awk语句以返回rhel-root的类型,但是我没有得到任何输出。

var=$(awk '{
  if($1 == "/dev/mapper/rhel-root"){
    print $2;
    }
  }' | df -Th | grep "^/dev")

如何将文件类型(xfs)分配给变量?

linux bash file filesystems
1个回答
1
投票

您需要将管道df .. grep ..的输出馈送到awk命令作为输入:

var=$(awk '{
  if($1 == "/dev/mapper/rhel-root"){
    print $2;
    }
  }' <(df -Th | grep "^/dev") )
echo "$var"

然后它将输出:xfs

广义的解决方案将是:

declare -A array                # assiciative array
while read -r fs type others; do
    if [[ $fs = /dev/* ]]; then
        array[$fs]="$type"      # associate fs with its type
    fi
done < <(df -Th)

echo "${array[/dev/mapper/rhel-root]}"
© www.soinside.com 2019 - 2024. All rights reserved.