HDD Auto Format/dev/sdb:
1, list all the disks on the current system, let the user choose, if you choose Quit to quit the script, if the user chooses the error, let the user choose again;
2, when the user selects, remind the user to confirm that the next operation may damage the data, and ask the user to confirm; if the user chooses Y to continue, n exits; otherwise, let the user re-select;
3. Erase all partitions on the hard drive (prompt, erase all partitions after the Sync command, and let the script sleep for 3 seconds before partitioning), and create three primary partitions for it, the first one is 20M, the second is 512M, the third is 128M, and the third is the swap partition type; Hint: The partition command is delivered to Fdisk via Echo)
#!/bin/bash
#
echo "Initial a disk ..."
Echo-e "\033[31mwarning:\033[0m"
Fdisk-l 2>/dev/null | Grep-o "^disk/dev/[sh]d[a-z]"
Read-p "Your choice:" Partdisk
if [$PARTDISK = = ' quit '-o $PARTDISK = = ' Q ']; Then
echo "Quit"
Exit 7
Fi
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 "would destroy all Data,continue:" CHOICE
until [$CHOICE = = ' y '-o $CHOICE = = ' n '];d o
Read-p "would destroy all Data,continue:" CHOICE
Done
if [$CHOICE = = ' n ']; Then
echo "Cancel partition, quiting ..."
Exit 9
Else
# # Auto Umount
For I in ' Mount | grep "/dev/sdb" | awk ' {print '} '; Do
fuser-km $I
Umount $I
echo "$I umount OK."
Done
# # Delete Disk partition
DD If=/dev/zero of= $PARTDISK bs=512 count=1 &>/tmp/log.txt
echo "Deleted partition, formating new partition ..."
Sync
Sleep 3
# # Format Disk
Echo ' n
P
1
+20m
N
P
2
+512m
N
P
3
+128m
T
3
82
W ' | Fdisk $PARTDISK >>/tmp/log.txt
Partprobe $PARTDISK
Sync
Sleep 3
# # Format Partition
Mke2fs-j ${partdisk}1 >>/tmp/log.txt |echo "format ${partdisk}1 finished."
Mke2fs-j ${partdisk}2 >>/tmp/log.txt |echo "format ${partdisk}2 finished."
Mkswap ${partdisk}3 >>/tmp/log.txt |echo "format swap ${partdisk}3 finished."
Fi
---end---
Linux command: Automatic format of hard disk