題目:用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