Job 3:
Requirements are as follows:
Write a script:
1, create a function, you can accept a disk device path (such as/DEV/SDB) as a parameter, before really start the next step to remind users of the risk, and let the user choose whether to continue;
Then empty all partitions on this disk device (prompt, use command dd If=/dev/zero of=/dev/sdb bs=512 count=1 Implementation, note that the device path is not written incorrectly); If this step fails, return 67 to the main program;
Then create two primary partitions on this disk device, one size is 100M, one size is 1G, and if this step fails, return 68 to the main program;
Format the two partitions, the file system type is ext3, and if this step fails, return 69 to the main program;
If the above process is normal, return 0 to the main program;
2, call this function, and by receiving the function to perform the return value to judge its performance, and display the information;
The code is as follows:
#!/bin/bash #Author: MOS #Script name:funPart.sh #Date & time:2012-10-10/12:20:53 #Version: 1.0.1 #Descri Ption: # Disk_mod () {#使用if匹配模式, use a for loop to determine whether there is, if so, take its absolute path, jump out of the loop, if the mismatch or the file does not exist, jump out of the function, return the IF [[N $ && $ =~] /DEV/SD[A-Z] "]];then local darray= (' ls/dev/sd[a-z]" to I in ${darray[*]};d o [["$i" = "$"]] ;& sd= $i && break do else return #当匹配成功, enter the selection, tell the user, whether to continue, the wrong words into the infinite loop, when the user select Y, then empty the target partition, and out of the Whil E Loop while:;d o read-p "Warning!!! This operation'll clean $Sd data. Next=y,quit=n [Y|n]: "Choice case $Choice in Y" dd If=/dev/zero of= $Sd bs=512 count=1 &am P;>/dev/null && Break | |
return 67;;
n) exit 88;;
*) echo "Invalid choice,please choice again."; Esac done #使用echo传递给fdisk进行分区, if this command fails, then jump out, error value 68, it should be noted that sometimes this return value is very strange, the author of the previous success or not is the return of 1, then after the restart, it is good, if prudent, you can create the partition, To judge, but you need to use other toolsInterception of related fields, although some small trouble, but no major obstacle echo-e "n\np\n1\n\n+100m\nn\np\n2\n\n+1024m\nw\n" |fdisk/dev/sdb &>/dev/null | | Return #格式化之前, let the kernel re-read the disk partition table, it is noteworthy that some system version, the use of partprobe invalid, such as the author's environment is rhel5.8, and rhel6.0 later, this command is very dangerous, and the use of partx-a/dev/ SDB is more effective ... This should be prudent, if the format fails, then tell the failed partition as a variable, and jump out of the function, and bring out the wrong value of the ' Partprobe ' part= ' fdisk-l/dev/$Sd |tail-2|cut-d ""-f1 ' F or M in ${part[*]};d o! Mke2fs-j $M &>/dev/null && errorpart= $M && return 0} #下面代码, call function, receive function back
Return value, based on the return value to determine what went wrong.
Disk_mod $ res=$? [$Res-eq 0] && exit 0 [$Res-eq] && echo error!
Invalid input. " [$Res-eq] && echo "error!
Command-> DD <-faild. " [$Res-eq] && echo "error!
Command-> fdisk <-faild. " [$Res-eq] && echo "error! Command-> mke2fs <-faild. "
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/