#!/bin/bash
#The script is used to mount cloud disk automatically.
#Date 2021-02-27
#检测命令是否执行成功
check_command() {
if [ $? -ne 0 ];then
echo "$1 command is error,please check again."
exit 1
fi
}
#检测输入参数是否为2
if [ $# -ne 2 ];then
echo "Please input two parameters."
exit 1
#检测第一个参数是否为设备文件
elif ! [ -b $1 ];then
echo "Please check the first parameter $1 is a disk dir again."
exit 1
#检测第二个参数是否为以根目录/开头文件
elif ! echo "$2" |grep -q '^/';then
echo "Please input the correct format,like: /dir "
exit 1
fi
#确认是否格式化磁盘
read -p "Are you sure you want to format $1 (y|n). " an
case $an in
y|Y)
echo "formatting $1"
mkfs -t ext4 $1
;;
n|N)
echo "Bye."
exit 1
;;
*)
echo "Please input y or n."
;;
esac
#挂载目录不存在则创建挂载目录
[ -d $2 ] || mkdir -p $2
#检测/etc/fstab配置文件是否已经存在该挂载点
n=`grep "$2" /etc/fstab |wc -l`
#不存在则配置挂载点
if [ "$n" -eq 0 ];then
echo "$1 $2 ext4 defaults 0 0" >> /etc/fstab
mount -a
check_command `echo 'mount'`
else
echo "The mount point $2 already exists in /etc/fstab configuraton file. "
exit 1
fi