shell脚本之挂载硬盘

#!/bin/bash
##############################################
#      how to use this shell                 #
#  command : ./howtomount.sh [args]          #
#  args:                                     #
#        None: you can mount or umount the   #
#              partition which is specified  #
#        all : mount all                     #
#      umount: umount all                    #
#     default: mount default partition Win7F #
#   default u: umount default partition      #
##############################################

##############################
# if path exist,if not,mkdir #
##############################
ifpathexist()
{
    if [ ! -x "$1" ];then
        mkdir "$1"
    fi
}

##############################
#   how to mount or umount   #
##############################
howtomount()
{
    case $1 in
        "1")
            echo "Win7 C:"
            path="/home/kehui/AllDisk/Win7C"
            if [ $# -eq 2 ];then               # input args's count judge
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda2 $path
            fi
        ;;
        "2")
            echo "Win7 D:"
            path="/home/kehui/AllDisk/Win7D"
            if [ $# -eq 2 ];then
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda1 $path
            fi
        ;;
        "3") # not found yet
            echo "Win7 E:"
            path="/home/kehui/AllDisk/Win7E"
            if [ $# -eq 2 ];then
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda1 $path
            fi
        ;;
        "4")
            echo "Win7 F:"
            path="/home/kehui/AllDisk/Win7F"
            if [ $# -eq 2 ];then
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda5 $path
            fi
        ;;
        "5")
            echo "Win7 G:"
            path="/home/kehui/AllDisk/Win7G"
            if [ $# -eq 2 ];then
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda6 $path
            fi
        ;;
        "6") # not found yet
            echo "XP C:"
            path="/home/kehui/AllDisk/XPC"
            if [ $# -eq 2 ];then
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda1 $path
            fi
        ;;
        "7")
            echo "Ubuntu Root"
            path="/home/kehui/AllDisk/UbtRoot"
            if [ $# -eq 2 ];then
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda4 $path
            fi
        ;;
        "8")
            echo "Ubuntu home"
            path="/home/kehui/AllDisk/UbtHome"
            if [ $# -eq 2 ];then
                echo 1 | sudo -S umount $path
            else
                ifpathexist $path
                echo 1 | sudo -S mount /dev/sda10 $path
            fi
        ;;
        *)
            echo "Error args.."
        ;;
    esac    
}

######################
# the main function  #
# the args judge     #
######################
if [ $# -lt 1 ];then
    echo "*******************"
    echo "Number of Partition"
    echo "1. Win7 C:"
    echo "2. Win7 D:"
    echo "3. Win7 E:"
    echo "4. Win7 F:"
    echo "5. Win7 G:"
    echo "6. XP C:"
    echo "7. Ubuntu Root"
    echo "8. Ubuntu Home"
    echo "9. Umount"
    echo "*******************"
    inputnum=
    echo -e "Please input the num you want to mount:\c"
    read inputnum
    if [ $inputnum -eq 9 ];then
        echo -e "Please input the num you want to umount:\c"
        read inputnum
        howtomount $inputnum "u"
    else
        howtomount $inputnum
    fi
else
    if [ "$1" = "all" ];then
        echo "all"
        howtomount 1 
        howtomount 2 
        #howtomount 3 
        howtomount 4 
        howtomount 5 
        #howtomount 6 
        howtomount 7 
        howtomount 8 
    elif [ "$1" = "umount" ];then
        echo "umount all"
        howtomount 1 "u"
        howtomount 2 "u"
        #howtomount 3 "u"
        howtomount 4 "u"
        howtomount 5 "u"
        #howtomount 6 "u"
        howtomount 7 "u"
        howtomount 8 "u"
    elif [ "$1" = "default" ];then
        echo "default"
        if [ "$2" = "m" ];then
            howtomount 4
        elif [ "$2" = "u" ];then
            howtomount 4 "u"
        else
            echo "Error args..."
        fi
    else
        echo "Error args."
    fi
fi


你可能感兴趣的:(shell)