这里介绍一个Linux 中使用shell脚本语言编辑的脚本,查看磁盘对应的编号和磁盘盘符,下面我们就来说一说关于查看linux磁盘空间的命令?我们一起去了解并探讨一下这个问题吧!

查看linux磁盘空间的命令(shell编写的脚本--查看物理磁盘对应的盘符)

查看linux磁盘空间的命令

这里介绍一个Linux 中使用shell脚本语言编辑的脚本,查看磁盘对应的编号和磁盘盘符。

这里服务器采用的是sda也就是系统的第一个盘符作为系统盘。所以磁盘序号从sdb开始作为系统的第一块磁盘,并且设置了磁盘的序号从数字0开始。也就是输入英文字符a会提示是系统盘,所以忽略掉了系统盘;输入英文字符b,会提示磁盘编号是0,并且是系统的第一块磁盘,输入英文字符c,会提示磁盘编号是1,并且是系统的第二块磁盘;当输入数字0的时候,会对应输出盘符是sdb。

下面是shell的脚本编写:

1.创建文件 touch test.sh

2.授予执行权限 chmod x test.sh

3.编辑脚本,脚本内容如下:

#! /bin/bash

echo "*******************"

echo "author:linxi"

echo "E-mail:630995935@qq.com"

echo "*******************"

num2disk(){

smcd=$1

if [ $smcd -ge 0 -a $smcd -le 24 ];then

declare -a disk_symbol

disk_symbol=(sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz)

echo This disk symbol is ${disk_symbol[$smcd]}

else

echo This number is out of range.

exit 0

fi

}

disk2num(){

member=$1

if [ `echo $member|grep ^[a-z]|awk '{print length($0)}'` -eq 1 ]; then

index=0

declare -a disk_mumber

disk_member=(b c d e f g h i j k l m n o p q r s t u v w x y z)

for i in ${disk_member[*]}

do

if [[ $member == $i ]]; then

echo sd$i symbol number is $index,是第`expr 1 $index`块数据硬盘.

return

fi

index=$(( $index 1))

done

if [ $member == 'a' ]; then

echo 'sda is system disk!!!'

fi

else

echo The bit of character is out of range,sd$member is not a disk symbol!

exit 0

fi

}

alnum=$1

if [ $# -eq 0 ]; then

echo "no args."

exit 0

else

if [ $# -eq 1 ]; then

echo "This is a arg."

if grep '^[[:digit:]]*$' <<< "$1"; then

num2disk $1

exit 0

else

echo $1 is not a number,sd$1 is a disk symbol.

fi

if grep '^[[:alpha:]]*$' <<< "$1"; then

disk2num $1

exit 0

else

echo 'This is not a character'

exit 0

fi

fi

echo "There is $# args."

exit 0

fi

4.执行脚本:

点斜杠加上脚本文件名,空格加上参数去执行:

./test.sh 参数。

举例:

./test.sh a

结果:

sda is system disk!!!

./test.sh b

sdb symbol number is 0,是第1块数据硬盘.

./test.sh 0

sdb symbol disk is sdb.

以上是执行脚本的过程和输出结果。

我认为这个脚本里面需要优化的是给数组进行for循环赋值的部分,感觉这里我是把盘符和英文字符去使用穷举法去依次列出,会影响程序的性能和效率。

鼓励话语:扛得住涅槃之痛,才配得上重生之美!

,