shell编程 自动创建分区

用shell脚本自动添加分区,已实现功能如下:

1.检查扩展分区是否,且是否可以添加,如果可以则把所有的剩余空间设置为扩展分区

2.当扩展分区存在,则输入要添加的分区大小,仅接受大于1的正整数

代码如下:

#!/bin/bash
Hard='/dev/sda'
Exten=`fdisk -l $Hard|grep Extended`
Maxnum=`fdisk -l $Hard|grep -o "^/dev/sda[1-9]\>"|tr -d [[:punct:]]|tr -d 'A-Za-z'|sort -n|tail -1`
if [[  -z $Exten ]];then
        if [[ $Maxnum -ge 4 ]];then
                echo "Disk partitions error!..."
                exit 1
        elif [ $Maxnum -eq 1 -o $Maxnum -eq 2 ];then
#       echo "1---3"
                cat << EOF
                e|E)use all free disk greate is Extended;
                *)Quit;
EOF
                Sdanum=$((Maxnum+1))
                read Opt
                case $Opt in
                e|E)
fdisk $Hard &> /opt/fdisk.log <<EOF
n
e
$Sdanum


w

EOF
                ;;
                *)
                echo "None operating ,Exit"
                exit 2
                ;;
                esac
        else
                cat << EOF
        e|E)use all free disk greate is Extended;
        *)Quit;
EOF
                read Opt
                case $Opt in
                e|E)
                fdisk $Hard &> /opt/fdisk.log <<EOF
                n
                e
               
               
                w
EOF

;;
                *)
                echo "None operating ,Exit"
                exit 2
                ;;
                esac

fi
else
        echo 'Please input new partition size(MB),Only number.'
        read Size
        Pun=`echo "$Size"|grep "[[:punct:]]\+"`
                while [[ $Size -le 1 || -n $Pun ]]
                do
                        echo "Wrong try again!"
                        Size=
                        read Size
                        Pun=`echo "$Size"|grep "[[:punct:]]\+"`
                done
        answer=${Size:-50}
        fdisk $Hard &> /opt/fdisk.log <<EOF
        n
       
        +${Size}M
        w

EOF
fi

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/cb56ac9ddb944a05ede6d0ea0d762653.html