shell編程風格樣例

#!/bin/bash
##filename:cleardisk.sh
##function:把硬盤置爲 空盤

DISK_NAME="$1"

###################################################
# 顯示幫助
###################################################
function show_help()
{
    echo "Usage: $0 /dev/sdx"
}

###################################################
# 參數檢查
###################################################
function chk_params()
{
    for cmd in lsscsi parted
    do
        if [ -z "$cmd" ]; then
            continue
        fi
        
        #檢查命令是否存在
        which $cmd > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo "${cmd} not exist"
            return 1
        fi        
    done

    #參數不能爲空
    if [ -z "${DISK_NAME}" ]; then
        show_help
        return 1
    fi
    
    #參數必須爲塊設備
    if [ ! -b "${DISK_NAME}" ]; then
        echo "${DISK_NAME} is not a block device."
        show_help
        return 1
    fi 
    
    #入參必須在硬盤列表中
    lsscsi | grep "\/dev" | grep -v DVD | grep -v -i Floppy | grep " ${DISK_NAME} " > /dev/null
    if [ $? -ne 0 ]; then
        echo "Error: ${DISK_NAME} is not disk."
        show_help
        return 1
    fi

    return 0
}

###################################################
# 判斷硬盤是否正在使用
###################################################
function disk_is_used()
{
    #mount 中不能包含 該設備
    mount | grep "^/dev/" | awk '{print $1}' | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by mount."
        return 1
    fi
    
    #df 中不能包含 該設備
    df -P -h | grep "^/dev" | awk '{print $1}' | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by df."
        return 1
    fi
    
    #swap 中不能包含 該設備
    swapon -s | grep "^/dev" | awk '{print $1}' | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by swap."
        return 1
    fi
    
    #正在格式化的硬盤
    ps -wwef | grep -E "\ mkfs\.|\/mkfs\." | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by mkfs."
        return 1
    fi
    
    #lvm 中不能包含 該設備
    which pvs > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        pvs | grep "${DISK_NAME}" > /dev/null
        if [ $? -eq 0 ]; then
            echo "Error: ${DISK_NAME} is used by lvm."
            return 1
        fi
    fi

    return 0
}

###################################################
# 把硬盤置爲 空盤
###################################################
function clear_disk()
{
    #刪除硬盤分區
    parted -s ${DISK_NAME} mklabel gpt
    if [ $? -ne 0 ]; then
        echo "parted -s ${DISK_NAME} mklabel gpt failed."
        return 1
    fi

    sleep 1
    
    #刷新系統信息
    partprobe ${DISK_NAME}
    if [ $? -ne 0 ]; then
        echo "partprobe ${DISK_NAME} failed."
        return 1
    fi
    
    sleep 1
    
    #刪除硬盤分區表
    dd if=/dev/zero of=${DISK_NAME} count=10 > /dev/null
    if [ $? -ne 0 ]; then
        echo "dd if=/dev/zero of=${DISK_NAME} count=10 failed."
        return 1
    fi

    return 0
}

###################################################
# 主函數
###################################################
function main()
{
    #參數檢查
    chk_params
    if [ $? -ne 0 ]; then
        return 1
    fi
    
    #判斷硬盤是否正在使用
    disk_is_used
    if [ $? -ne 0 ]; then
        return 1
    fi
    
    #把硬盤置爲 空盤
    clear_disk
    if [ $? -ne 0 ]; then
        return 1
    fi
    
    echo "Set ${DISK_NAME} to blank successfully."

    return 0
}

main "$@"
exit $?
相關文章
相關標籤/搜索