shell自动实现磁盘分区挂载

利用shell脚本实现对一个磁盘进行分区格式化挂载

#!/bin/bash

#
echo "A disk is to init"
fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"  //筛选出来现在Linux中的所有的磁盘其中包括IDE和SATA硬盘
read -p "Your choice: " choice       //选在要进行操作的硬盘名称
until [ $choice != 'q' -a $choice != 'Q' -a $choice != 'quit' -a $choice != 'Quit' ]&& fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"|grep $choice;do //判断选择如果不是退出指令的话就继续
if [ $choice == 'q' -o $choice == 'Q' -o $choice == 'quit' -o $choice == 'Quit' ];then  echo "The shell is quiting"
         exit 5
else
        echo "Wrong options"
        read -p "Your choice: " choice
fi
done
        echo "Disk $choice is to init,this will destroy all data.Are you sure"
        read  -p "Continue? Y|n or N|n:  " CHOICE
if [ $CHOICE == 'N' -o $CHOICE == 'n' ];then      //在进行操作之前等待客户进行确认,如果输入的是Y|y或者是N|n的话那么进行相应的操作
        echo "You chose no"
        echo "The shell is quiting"
        exit 5
elif [ $CHOICE == 'Y' -o $CHOICE == 'y' ];then
        echo "You chose yes,init will begin"
        echo "initing....please wait"
        dd if=/dev/zero of=$choice bs=512 count=1 &>/dev/null   //将硬盘原来的分区全部删除掉,并且进行分区的重建
        echo "n
        p
        1


        +5G
        n
        p
        2


        +5G
        w" | fdisk $choice &>/dev/null
else
        echo "Wrong choice Y|y or N|n"
fi
partprobe $choice
sync
sleep 3
mkfs -t ext4 ${choice}1 &>/dev/null     //格式化硬盘并挂载
mkfs -t ext4 ${choice}2 &>/dev/null  
mkdir /data1 &>/dev/null
mkdir /data2 &>/dev/null
mount ${choice}1 /data1
mount ${choice}2 /data2
echo "Init finish"

你可能感兴趣的:(shell)