標籤:shell指令碼完成磁碟分割格式化
馬哥教育視頻學習筆記-shell指令碼完成磁碟分割格式化-http://edu.51cto.com/lesson/id-28201.html
以下是指令碼內容,內容比較簡單,不做詳解
在Linux上準備一塊閒置磁碟即可,如何分區可以在指令碼內改下
#!/bin/bash
#
echo "Initial a disk..."
echo -e "\033[31mWarning: \033[0m "
#開始的說明資訊,其中Warning以紅色字型顯示
fdisk -l 2> /dev/null | grep -o "^Disk /dev/[sh]d[a-z]"
#顯示所有磁碟裝置
read -p "Your choice(Disk or quit):" PARTDISK
if [ $PARTDISK == ‘quit‘ ]; then
echo "quit"
exit 7
fi
for I in `mount | grep "$PARTDISK" | awk ‘{print $1}‘`;do
fuser -km $I
umount $I
echo "$I umount ok."
done
until fdisk -l 2> /dev/null | grep -o "^Disk /dev/[sh]d[a-z]" | grep "Disk $PARTDISK$" &> /dev/null;do
read -p "Wrong option,Your choice again:" PARTDISK
done
read -p "Will destroy all data, continue(y or n):" CHOICE
until [ $CHOICE == ‘y‘ -o $CHOICE == ‘n‘ ]; do
read -p "Will destroy all data, continue(y or n):" CHOICE
done
if [ $CHOICE == ‘n‘ ]; then
echo "Quit"
exit 9
else
dd if=/dev/zero of=$PARTDISK bs=512 count=1 &> /dev/null
sync
sleep 3
echo ‘n
p
1
+2G
n
p
2
+3G
n
p
3
+1G
w‘ | fdisk $PARTDISK &> /dev/null
partprobe $PARTDISK
sync
sleep 2
mkfs.ext4 ${PARTDISK}1 &> /dev/null
mkfs.ext4 ${PARTDISK}2 &> /dev/null
mkswap ${PARTDISK}3 &> /dev/null
fi
fdisk -l $PARTDISK
#查看分區結果
本文出自 “汪立明” 部落格,請務必保留此出處http://80cto.blog.51cto.com/7503144/1611078
馬哥教育視頻學習筆記-shell指令碼完成磁碟分割格式化