自動建立分區的shell指令碼

來源:互聯網
上載者:User

題目:用shell指令碼自動添加分區,已實現功能如下:

1.檢查擴充分區是否,且是否可以添加,如果可以則把所有的剩餘空間設定為擴充分區

2.當擴充分區存在,則輸入要添加的分區大小,僅接受大於1的正整數

代碼如下:

#!/bin/bashHard='/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 -1if [[  -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 <<EOFne$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                                                      wEOF                    ;;                *)                echo "None operating ,Exit"                exit 2                ;;                esac            fielse        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    EOFfi
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.