Shell script for automatically creating partitions

Source: Internet
Author: User

Topic: Automatically add partitions with shell script, implemented functions as follows:

1. Check that the extended partition is, and can be added, and if possible, set all remaining space to an extended partition

2. When an extended partition exists, enter the size of the partition to be added, accepting only positive integers greater than 1

The code is as follows:

#!/bin/bash hard= '/dev/sda ' exten= ' fdisk-l $Hard |grep Extended ' maxnum= ' fdisk-l $Hard |grep-o "^/dev/sda[1-9]\>" |tr
                -D [[:p unct:]]|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 # Ech O "1---3" cat << EOF e|
                E use the 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 the all free disk greate is Extended;
*) Quit; EOF Read OPT case $OptIn 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) with only number. '
                Read Size pun= ' echo ' $Size "|grep" [[:p unct:]]\+] ' while [[$Size-le 1 | |-n $PUN]]
                        Do echo "Wrong try again!" size= read Size pun= ' echo ' $Size ' |grep ' [[:p unct:]]\+ ' do Ne answer=${size:-50} fdisk $Hard &>/opt/fdisk.log <<eof n +${si Ze}m W EOF Fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.