注解:一键挂载磁盘脚本,默认挂载data目录
#!/bin/bash

#磁盘文件夹名称
Data="data"

if [ `whoami` != 'root' ];then
        echo -e "\033[31m 请以root用户执行,Please run as root!\033[0m"
        exit 1
fi
clear


function manual_add(){
        lsblk | grep /$Data
        if [ $? -eq 0 ];then
                echo "磁盘已挂载"
                exit 1
        else
                lsblk
        fi
        #获取新加的磁盘盘符
        read -p "请输入你新添加硬盘的盘符,例如sda或sdb: " DISK_NAME
        echo "查看$Data"
        ls / | grep $Data
        if [ $? -eq 0  ];then
                mv /$Data  /bak
                mkfs.ext4 -F /dev/${DISK_NAME}
                [ -d /$Data ] || mkdir /$Data
                cp /etc/fstab{,.bak}
                echo -e "UUID=$(blkid /dev/${DISK_NAME} | awk '{print $2}' | awk -F\" '{print $2}') /$Data ext4 defaults   0 2" >> /etc/fstab
                mount -a && echo -e "\033[32m硬盘挂载成功!\033[0m" || echo -e "\033[31m硬盘挂载失败!\033[0m"
                chmod 777 /data
                echo "正在转移文件"
                mv /bak/* /$Data/  &> /dev/null 
                ls /$Data/
                echo "转移完成"
        else
                mkfs.ext4 -F /dev/${DISK_NAME}
                [ -d /$Data ] || mkdir /$Data
                cp /etc/fstab{,.bak}
                echo -e "UUID=$(blkid /dev/${DISK_NAME} | awk '{print $2}' | awk -F\" '{print $2}') /$Data ext4 defaults   0 2" >> /etc/fstab
                mount -a && echo -e "\033[32m硬盘挂载成功!\033[0m" || echo -e "\033[31m硬盘挂载失败!\033[0m"
                chmod 777 /$Data
        fi
}
manual_add