#!/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 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 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