需求:
- 1、列出系统所有磁盘,并输入对应的操作磁盘,输入“quit”退出脚本,输入错误提示重新输入;
- 2、输入正确磁盘后,需要用户再次确认操作,输入“y”进行磁盘初始化,输入“n”退出脚本,输入错误提示重新输入;
- 3、对磁盘进行划分两个主分区,分别为20M和50M,在操作之前先删除磁盘所有数据;
脚本:
#!/bin/bash
#
echo "该脚本是对磁盘进行初始化,会导致数据丢失,操作请慎重!!!"
fdisk -l | grep '^Disk /dev/sd[a-z]' | cut -d':' -f1
read -p "Please select disk :" STRING
if [ $STRING == 'quit' ]; then
\techo "quit"
\texit 2
fi
until fdisk -l | grep '^Disk /dev/sd[a-z]' | cut -d':' -f1 | grep "^Disk $STRING$"; do
\techo "Input error "
\tread -p "Again select disk :" STRING
done
read -p "Please sure your choice's disk :" CHOICE
until [ $CHOICE == 'y' -o $CHOICE == 'n' ]; do
\techo "Again input"
\tread -p "Again your choice's disk :" CHOICE
done
if [ $CHOICE == 'n' ]; then
\techo "quit"
\texit 3
else
\tdd if=/dev/zero of=/dev/sdb bs=512 count=1
\tsync
\tsleep 3
\techo 'n
\tp
\t1
\t+20M
\tn
\tp
\t2
\t+500M
\tw' | fdisk $STRING & >/dev/null
fi
运行脚本:
![Shell脚本100例:35 使用脚本自动对磁盘进行初始化](http://p2.ttnews.xyz/loading.gif)
查看分区信息
![Shell脚本100例:35 使用脚本自动对磁盘进行初始化](http://p2.ttnews.xyz/loading.gif)
閱讀更多 運維小筆記 的文章