掛載與卸載磁盤分區

昨天裝了一個XFCE桌面環境,感覺不錯,不過,有點不方便之處,就是在XFCE窗口管理下竟然看不到其他沒有掛載的分區圖標,無法在GUI下直接點擊掛載;在GNOME下反而很容易掛載,在Ubuntu下甚至不用輸入管理員密碼。

爲了方便以後掛載與卸載其他沒有掛載的分區,只好自己動手寫一個shell腳本了。
以下是掛載與卸載分區的腳本。這是我的情況,如果你也想用的話,可能要修改一下。下面的腳本沒有做任何信息隱藏,可以會暴露我的計算機裏面的一些信息,不過沒關係;如果我的計算機真的因爲這被入侵或信息泄露,那說明我現在是技不如人!呵呵......

爲了使這些腳本即使在純終端下也能正常運行並不會出現亂碼現象,在腳本中全部使用了ASCII中的英文字符進行書寫,儘管註釋不會被執行,爲了保持一致,連同註釋一併使用英文。由於本人英文水平有限,可能某些地方表述的不是太準確,望見諒!
一、掛載
#! /bin/bash
#
# SCRIPT: mpart --------mount the partition
# AUTHOR: xgfone
# DATE:   10/12 2011
# REV:    0.1.P (Valid are A, B, D, T, Q, and P)
#               (For Alpha, Beta, Dev, Test, QA, and Production)
#
# PLATFORM: Linux.  Other platform don't be tested!
#         (SPECIFY: AIX, HP-UX, Linux, OpenBSD, Solaris, other flavor,
#                   or Not platform dependent)
#
# REQUIREMENTS: the commands of using in this script: mount, mkdir
#
# PURPOSE: Batch mount the partition which don't be mounted when the
#          system has been started.
#
# NOTE: this scrip has nine directiories, which can mount nine
#       partitions. If you has less than nine partitions, you can only
#       assign what you demand to the directory variable which express
#       the name of some of nine partitions; and comment the others.
#       But if the number of your partitions to mount is more than
#       nine, you need create the new directiory variable, and add the
#       correspondent statements to the correspondent positions.
#       Just in one word: ACCORDING TO YOUR REQUESTS!
#
# EMAIL: [email protected]
#
# REV LIST:
#        DATE: 10/12 2011
#        BY:   xgfone
#        MODIFICATION: first create!
#
# set -n   # Uncomment to check script syntax, without execution.
#          # NOTE: Do not forget to put the # comment back in or
#          #       the shell script will never execute!
# set -x   # Uncomment to debug this shell script
#
################################################################################
#                          COPYRIGHT
################################################################################
#    mpart --------mount the partitions
#    Copyright (C) 2011  xgfone
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
######################################################################
#        const variable
#####################################################################
const_winxp=/dev/sda1
const_debian=/dev/sda2
const_D_programming=/dev/sda5
const_G_interim=/dev/sda6
const_I_video=/dev/sda7
const_Ubuntu=/dev/sda8
const_E_xgfone=/dev/sdb1
const_H_software=/dev/sdb5
const_series=/dev/sdb6

m_mymount=mymount
m_winxp=winxp
m_debian=debian
m_D_programming=D_programming
m_G_interim=G_interim
m_I_video=I_video
m_Ubuntu=Ubuntu
m_E_xgfone=E_xgfone
m_H_software=H_software
m_series=series
######################################################################
#                create the mounted directiories
######################################################################
# create the directories to be used to mount the partition.
# the p option makes sure that it don't have error if the directories
# that you will create have already existed.

# create the main directiory
mkdir -p ~/$m_mymount

# create the mounted subdirectories
mkdir -p ~/$m_mymount/$m_winxp
mkdir -p ~/$m_mymount/$m_debian
mkdir -p ~/$m_mymount/$m_D_programming
mkdir -p ~/$m_mymount/$m_G_interim
mkdir -p ~/$m_mymount/$m_I_video
mkdir -p ~/$m_mymount/$m_Ubuntu
mkdir -p ~/$m_mymount/$m_E_xgfone
mkdir -p ~/$m_mymount/$m_H_software
mkdir -p ~/$m_mymount/$m_series
#######################################################################
#                mount the partition
#######################################################################
# mount all partition at once without the query
function mount_all_disk
{
    echo "mount $m_winxp......"
    sudo mount /dev/sda1 ~/$m_mymount/$m_winxp
    echo "mount $m_debian......"
    sudo mount /dev/sda2 ~/$m_mymount/$m_debian
    echo "mount $m_D_programming......"
    sudo mount /dev/sda5 ~/$m_mymount/$m_D_programming
    echo "mount $m_G_interim......"
    sudo mount /dev/sda6 ~/$m_mymount/$m_G_interim
    echo "mount $m_I_video......"
    sudo mount /dev/sda7 ~/$m_mymount/$m_I_video
    echo "mount $m_Ubuntu......"
    sudo mount /dev/sda8 ~/$m_mymount/$m_Ubuntu
    echo "mount $m_E_xgfone......"
    sudo mount /dev/sdb1 ~/$m_mymount/$m_E_xgfone
    echo "mount $m_H_software......"
    sudo mount /dev/sdb5 ~/$m_mymount/$m_H_software
    echo "mount $m_series......"
    sudo mount /dev/sdb6 ~/$m_mymount/$m_series
}

#ask whether you will mount it when you mount each disk
function ask_mount_each_disk
{
    echo -n "mount $1?(y or n) "
    check_y_or_n=""
    read check_y_or_n
    while :
    do
        if [ check_y_or_n == "y" || check_y_or_n == "Y" ] ;then
            echo "mount $1......"
            sudo mount /dev/sda1 ~/$m_mymount/$1
            break
        elif [ check_y_or_n == "n" ] || [ check_y_or_n == "N" ];then
            break   #nothint to do
        else
            echo -n "input error.It must be \"y\" or \"n\"! "
            echo -n "Please input again!(y or n) "
            read check_y_or_n
        fi
    done
}

#mount partitions
echo -n "mount all partition at once?(y or n) "
check_y_or_n=""
read check_y_or_n
while :
do
    if [ $check_y_or_n == "y" ] || [ $check_y_or_n == "Y" ];then
        mount_all_disk
        break
    elif [ $check_y_or_n == "n" ] || [ $check_y_or_n == "N" ];then
        ask_mount_each_disk  $m_winxp
        ask_mount_each_disk  $m_debian
        ask_mount_each_disk  $m_D_programming
        ask_mount_each_disk  $m_G_interim
        ask_mount_each_disk  $m_I_video
        ask_mount_each_disk  $m_Ubuntu
        ask_mount_each_disk  $m_E_xgfone
        ask_mount_each_disk  $m_H_software
        ask_mount_each_disk  $m_series
        break
    else
        echo -n "input error.It must be \"y\" or \"n\"! "
        echo -n "Please input again! (y or n) "
        read check_y_or_n
    fi
done

二、卸載
#! /bin/bash
#
# SCRIPT: umpart --------umount the partition
# AUTHOR: xgfone
# DATE:   10/12 2011
# REV:    0.1.P (Valid are A, B, D, T, Q, and P)
#               (For Alpha, Beta, Dev, Test, QA, and Production)
#
# PLATFORM: Linux.  Other platform don't be tested!
#         (SPECIFY: AIX, HP-UX, Linux, OpenBSD, Solaris, other flavor,
#                   or Not platform dependent)
#
# REQUIREMENTS: the commands of using in this script: umount
#
# PURPOSE: Batch umount the partitions which have been mounted.
#
# NOTE: this scrip has nine directiories, which can mount nine
#       partitions. If you has less than nine partitions, you can only
#       assign what you demand to the directory variable which express
#       the name of some of nine partitions; and comment the others.
#       But if the number of your partitions to mount is more than
#       nine, you need create the new directiory variable, and add the
#       correspondent statements to the correspondent positions.
#       Just in one word: ACCORDING TO YOUR REQUESTS!
#
# EMAIL: [email protected]
#
# REV LIST:
#        DATE: 10/12 2011
#        BY:   xgfone
#        MODIFICATION: first create!
#
# set -n   # Uncomment to check script syntax, without execution.
#          # NOTE: Do not forget to put the # comment back in or
#          #       the shell script will never execute!
# set -x   # Uncomment to debug this shell script
#
################################################################################
#                          COPYRIGHT
################################################################################
#    umpart --------umount the partitions
#    Copyright (C) 2011  xgfone
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
######################################################################
#        const variable
#####################################################################
m_mymount=mymount
m_winxp=winxp
m_debian=debian
m_D_programming=D_programming
m_G_interim=G_interim
m_I_video=I_video
m_Ubuntu=Ubuntu
m_E_xgfone=E_xgfone
m_H_software=H_software
m_series=series
#######################################################################
#                umount the partition
#######################################################################
# umount all partition at once without the query
function umount_all_disk
{
    echo "umount $m_winxp......"
    sudo umount  ~/$m_mymount/$m_winxp
    echo "umount $m_debian......"
    sudo umount  ~/$m_mymount/$cosnt_debian
    echo "umount $m_D_programming......"
    sudo umount  ~/$m_mymount/$m_D_programming
    echo "umount $m_G_interim......"
    sudo umount  ~/$m_mymount/$m_G_interim
    echo "umount $m_I_video......"
    sudo umount  ~/$m_mymount/$m_I_video
    echo "umount $m_Ubuntu......"
    sudo umount  ~/$m_mymount/$m_Ubuntu
    echo "umount $m_E_xgfone......"
    sudo umount  ~/$m_mymount/$m_E_xgfone
    echo "umount $m_H_software......"
    sudo umount  ~/$m_mymount/$m_H_software
    echo "umount $m_series......"
    sudo umount  ~/$m_mymount/$m_series
}

#ask whether you will umount it when you umount each disk
function ask_mount_each_disk
{
    echo -n "umount $1?(y or n) "
    check_y_or_n=""
    read check_y_or_n
    while :
    do
        if [ check_y_or_n == "y" || check_y_or_n == "Y" ] ;then
            echo "umount $1......"
            sudo umount  ~/$m_mymount/$1
            break
        elif [ check_y_or_n == "n" ] || [ check_y_or_n == "N" ];then
            break   #nothint to do
        else
            echo -n "input error.It must be \"y\" or \"n\"! "
            echo -n "Please input again!(y or n) "
            read check_y_or_n
        fi
    done
}

#umount partitions
echo -n "umount all partition at once?(y or n) "
check_y_or_n=""
read check_y_or_n
while :
do
    if [ $check_y_or_n == "y" ] || [ $check_y_or_n == "Y" ];then
        umount_all_disk
        break
    elif [ $check_y_or_n == "n" ] || [ $check_y_or_n == "N" ];then
        ask_umount_each_disk $m_winxp
        ask_umount_each_disk $m_debian
        ask_umount_each_disk $m_D_programming
        ask_umount_each_disk $m_G_interim
        ask_umount_each_disk $m_I_video
        ask_umount_each_disk $m_Ubuntu
        ask_umount_each_disk $m_E_xgfone
        ask_umount_each_disk $m_H_software
        ask_umount_each_disk s$m_series
        break
    else
        echo -n "input error.It must be \"y\" or \"n\"! "
        echo -n "Please input again! (y or n) "
        read check_y_or_n
    fi
done
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章