自动分区并格式化某块硬盘的BASH脚本

Posted 季宁电脑笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动分区并格式化某块硬盘的BASH脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#

fdisk -l | grep "^Disk /dev/[sh]d[a-z]" | awk  ‘{print $2}‘ | sed "[email protected]:@@g"
read -p "Your choice PARTDISK above:" PARTDISK
if [ $PARTDISK == quit ];then
  echo "quit..."
  exit 0
fi
until fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]" | grep "^Disk ${PARTDISK}$"&>/dev/null ;do
  read -p "wrong choice Your choice again:" PARTDISK
done
  read -p "Will destroy all data continue[y/n]:" CHOICE
until [ $CHOICE == "y" -o $CHOICE == "n" ];do
  read -p "Will destroy all data continue[y/n]:" CHOICE
done
[ $CHOICE == n ] && echo "quit..." && exit 0;
for DISK in `mount | grep "/dev/sdb" | awk ‘{print $1}‘`;do
fuser -km $DISK
umount $DISK && echo "$DISK umount ok"
done
dd if=/dev/zero of=$PARTDISK bs=512 count=1&>/dev/null
partprobe $PARTDISK
sync&>/dev/null
sleep 2
echo ‘n
p
1

+20M
n
p
2

+512M
n
p
3

+128M
t
3
82
w‘|fdisk $PARTDISK &>/dev/null
partprobe $PARTDISK
sync &>/dev/null
sleep 2
mke2fs ${PARTDISK}1 &>/dev/null && echo "${PARTDISK}1finished"
sync &>/dev/null
sleep 2
mke2fs ${PARTDISK}2 &>/dev/null && echo "${PARTDISK}2finished"
ssync &>/dev/null
sleep 2
mkswap ${PARTDISK}3 &>/dev/null && echo "${PARTDISK}3finished"
sync &>/dev/null
sleep 2

  

以上是关于自动分区并格式化某块硬盘的BASH脚本的主要内容,如果未能解决你的问题,请参考以下文章

shell编程:自动格式化硬盘并挂载

脚本自动分区,格式化,挂载

监控指定硬盘分区的使用率的脚本

一键自动创建磁盘分区以及格式化挂载脚本

Linux命令:硬盘自动格式化

ubuntu16.04对硬盘进行分区格式化并设置开机自动挂载