shell脚本实现MBR、GPT自动分区及格式化

来源:互联网 发布:编程谜题 pdf 编辑:程序博客网 时间:2024/06/05 19:25

这是一个小脚本:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

#!/bin/bash##Author:samrtwy(qq:120649294)#Version:2.0#Date:2017-04-23#Descripton:According to the input partition table mode,disk,partition number,#           size,file system type of an empty disk for sutomatic partitioning.read  -p "Please input partition table mode ( 1:GPT | 2:MBR ) :" MKLBread  -p "Please input device pathi ( exp:/dev/sdb ) :" DEPTread  -p "Please input number of partitions :" PANUread  -p "Please input partition size ( default M ) :" PASZread  -p "Please input partition type (ext3,ext4,xfs):" PTYPE[[ -b ${DEPT} ]] || echo "Input is not device block!";exit 1MP="mkpart"PT="parted"PY="primary" TMP_S=0 #partition size startTMP_E=0 #partition size end[[ "${PTYPE}" == "xfs" ]] && OP="-f"if [[ "$MKLB" == "1" ]] || [[ "$MKLB" == "GPT" ]];then    MKLB="gpt"    ${PT} -s ${DEPT} mklabel ${MKLB}    for (( i=1;i<=${PANU};i++ ))    do            TMP_E=$((PASZ+TMP_E))            ${PT} -s ${DEPT} ${MP} ${PY} ${TMP_S} ${TMP_E}            sleep 1            mkfs.${PTYPE} ${OP} ${DEPT}${i} &>/dev/null || echo "${DEPT}${i} format error !"             TMP_S=$((TMP_E+1))    doneelif [[ "$MKLB" == "2" ]] || [[ "$MKLB" == "MBR" ]];then    MKLB="msdos"    ${PT} -s ${DEPT} mklabel ${MKLB}    if [ ${PANU} -le 3 ]    then    for (( i=1;i<=3;i++ ))    do            TMP_E=$((PASZ+TMP_E))            ${PT} -s ${DEPT} ${MP} ${PY} ${TMP_S} ${TMP_E}            sleep 1            mkfs.${PTYPE} ${OP} ${DEPT}${i} &>/dev/null || echo "${DEPT}${i} format error !"             TMP_S=$((TMP_E+1))    done    fi    if [ ${PANU} -eq 4 ]    then        for (( i=1;i<=4;i++ ))        do            if [ $i -eq 4 ]            then                 TMP_E=$((PASZ+TMP_E))                 fdisk ${DEPT}<<EOF &>/dev/null                n                e                $((TMP_E*1024+1))                wEOF                 exit 0            fi            TMP_E=$((PASZ+TMP_E))            ${PT} -s ${DEPT} ${MP} ${PY} ${TMP_S} ${TMP_E}            sleep 1            mkfs.${PTYPE} ${OP} ${DEPT}${i} &>/dev/null || echo "${DEPT}${i} format error !"             TMP_S=$((TMP_E+1))        done    fi    if [ ${PANU} -gt 4 ]    then        for (( i=1;i<=${PANU};i++ ))        do            if [ $i -eq 4 ]            then                 fdisk ${DEPT}<<EOF &>/dev/null                n                e                $(($((TMP_E+PASZ))*1024+1))                wEOF                 partprobe &>/dev/null                 partx -a ${DEPT} &>/dev/null                 let i=i+1            fi            if [ $i -gt 4 ]            then                for (( j=5;j<=${PANU};j++ ))                do                    TMP_S=$((TMP_E+1))                    TMP_E=$((PASZ+TMP_E))                    ${PT} -s ${DEPT} ${MP} logical ${TMP_S} ${TMP_E}                    sleep 1                    mkfs.${PTYPE} ${OP} ${DEPT}${j} &>/dev/null || echo "${DEPT}${j} format error !"                     TMP_S=$((TMP_E+1))                    if [ $j -eq ${PANU} ]                    then                        exit 0                    fi                done            fi            TMP_E=$((PASZ+TMP_E))            ${PT} -s ${DEPT} ${MP} ${PY} ${TMP_S} ${TMP_E}            sleep 1            mkfs.${PTYPE} ${OP} ${DEPT}${i} &>/dev/null || echo "${DEPT}${i} format error !"             TMP_S=$((TMP_E+1))        done    fielse    echo "Input ERROR!"fi
0 0
原创粉丝点击