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"
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值