Shell automatically clears the disk created and formatted
Source: Internet
Author: User
Shell automatically clears the disk creation and formatting requirements as follows: write a script: 1. create a function and take a disk device path (such as/dev/sdb) as the parameter; before you start the next step, remind the user of the danger and ask the user to choose whether to continue. then...
Shell automatically clears the disk creation and formatting requirements as follows: write a script: 1. create a function and take a disk device path (such as/dev/sdb) as the parameter; remind the user of danger before the subsequent steps are actually started, and ask the user to choose whether to continue; then clear all partitions on the disk device (prompt, run the command dd if =/dev/zero of =/dev/sdb bs = 512 count = 1. Note that the device path must not be written incorrectly.) if this step fails, return 67 to the main program; www.2cto.com
Create two primary partitions on the disk device. one is 100 MB and the other is 1 GB. if this step fails, return 68 to the primary program. format the two partitions, the file system type is ext3; 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; the execution status is determined by receiving the return value of the function execution, and the information is displayed. 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 # Description: # Disk_Mod () {# Use the if matching mode, and use the for loop to determine whether the request exists, if yes, take its absolute path and jump out of the loop. If no match exists
Or the file does not exist. if you exit the function, 66 if [[-n $1 & $1 = ~ is returned ~ "/Dev/sd [a-z]"]; then local Darray = ('ls/dev/sd [a-z] ') for I in $ {Darray [*]}; do [["$ I" = "$1"] & Sd = $ I & break done else return 66 fi # When the match is successful, go to the selection page and tell the user, whether to continue or not. If an error is entered, an infinite loop is entered. when the user selects Y, the target partition is cleared and the while loop www.2cto.com while:; do read-p "Warning !!! This operation will clean $ Sd data. next = y, Quit = n [y | n]: "Choice case $ Choice in y) dd if =/dev/zero of = $ Sd bs = 512 count = 1 &>/dev/null & break | return 67; n) exit 88 ;;*) echo "Invalid choice, please choice again. "; esac done # use echo to pass to fdisk for partitioning. if this command fails, it will jump out. the error value is 68. Note that sometimes this return value is very strange, the previous success or failure of the author is the return of 1. after the restart, it will be fine. if you are careful, you can judge the created partition, but you need to use other tools to intercept related fields, echo-e "n \ np \ n1 \ n + 100M \ nn \ n P \ n2 \ n + 1024M \ nw \ n "| fdisk/dev/sdb &>/dev/null | return 68 # before formatting, to re-read the disk partition table from the kernel, it is worth noting that in some system versions, the use of partprobe is invalid. for example, the author's environment is rhel5.8, and after rhel6.0, this command is very dangerous, the use of partx-a/dev/sdb is better... this item should be careful. if the formatting fails, it indicates that the failed partition is defined as a variable and the function exists, and bring out the error value 69 www.2cto.com 'partprobe' Part = 'fdisk-l/dev/$ Sd | tail-2 | cut-d ""-f1 'for M in $ {Part [*]}; do! Mke2fs-j $ M &>/dev/null & ErrorPart = $ M & return 69 done return 0} # The following code calls a function and receives the return value of the function, determine the error based on the returned value. Disk_Mod $1 Res = $? [$ Res-eq 0] & exit 0 [$ Res-eq 66] & echo "Error! Invalid input. "[$ Res-eq 67] & echo" Error! Command-> dd <-Faild. "[$ Res-eq 68] & echo" Error! Command-> fdisk <-Faild. "[$ Res-eq 69] & echo" Error! Command-> mke2fs <-Faild ."
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service