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