这里介绍一个Linux 中使用shell脚本语言编辑的脚本,查看磁盘对应的编号和磁盘盘符。 这里服务器采用的是sda也就是系统的第一个盘符作为系统盘。所以磁盘序号从sdb开始作为系统的第一块磁盘,并且设置了磁盘的序号从数字0开始。也就是输入英文字符a会提示是系统盘,所以忽略掉了系统盘;输入英文字符b,会提示磁盘编号是0,并且是系统的第一块磁盘,输入英文字符c,会提示磁盘编号是1,并且是系统的第二块磁盘;当输入数字0的时候,会对应输出盘符是sdb。
下面是shell的脚本编写:
1.创建文件 touch n2d.sh
2.授予执行权限 chmod +x n2d.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
for i in {b..z};do
disk_symbol[${#disk_symbol[*]}]=sd$i
done
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
for i in {b..z};do
disk_member[${#disk_member[*]}]=$i
done
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:]]*#39; <<< "$1"; then
num2disk $1
exit 0
else
echo $1 is not a number,sd$1 is a disk symbol.
fi
if grep '^[[:alpha:]]*#39; <<< "$1"; then
disk2num $1
exit 0
else
echo 'This is not a character'
exit 0
fi
fi
echo "There is $# args,please input only one arg."
exit 0
fi
4.执行脚本:
点斜杠加上脚本文件名,空格加上参数去执行:
./n2d.sh 参数。
举例:
./n2d.sh a
结果:
sda is system disk!!!
./n2d.sh b
sdb symbol number is 0,是第1块数据硬盘.
./n2d.sh 0
sdb symbol disk is sdb.
以上是执行脚本的过程和输出结果。
5.我在这个脚本优化的是给数组进行for循环赋值的部分:
declare -a disk_symbol
for i in {b..z};do
disk_symbol[${#disk_symbol[*]}]=sd$i
done
echo This disk symbol is ${disk_symbol[$smcd]}
通过使用for循环的方法,对已经定义的数组中的元素进行了赋值。
鼓励话语:故乡容不下肉身,他乡容不下灵魂。总之,是人在江湖,身不由己!英雄征服世界,圣人征服自我!
本文暂时没有评论,来添加一个吧(●'◡'●)