假设磁盘上只有主分区,那么查找当前分区数的最佳方法是什么?
有没有比以下更好的方法:
fdisk -l > temp
#Following returns first column of the last line of temp e.g. /dev/sda4
lastPart=$(tail -n 1 temp | awk '{print $1}')
totalPartitions=$(echo ${lastPart:8})
Run Code Online (Sandbox Code Playgroud)
$totalPartitions变量有时返回NULL。这就是为什么,我想知道是否有更可靠的方法来查找当前分区数。
关于什么:
totalPartitions=$(grep -c 'sda[0-9]' /proc/partitions)
Run Code Online (Sandbox Code Playgroud)
?
(哪里sda是你感兴趣的磁盘名称,酌情替换)