用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
新手,代碼多有不足,歡迎各位大神多指正