Shell script for disk partition formatting

Source: Internet
Author: User
Tags echo command

Shell script for disk partition formatting

Our shell scripting structure is divided into sequential structure, selection structure and loop structure, we have previously introduced the selection structure including if, case statements, loop structure including for loop, while loop and until loop, today we will introduce the until loop, The syntax format for the until loop is as follows:

Until Condition;do

Loop body

Done

As can be seen from the above, the syntax format of our until loop is the same as our while loop, the only difference is that our while loop enters the loop body in the case of satisfying condition, And our until is to terminate the loop in the case of satisfying condition, which is just the opposite of our while loop, and until can be directly connected to our Linux command, which means that the command is seen as a condition for jumping out of the loop. Until determines the execution status return value of the command, and any time we want to refer to the execution state return value of the command, we can write the command directly behind the program keyword.

Exercise: Write a script that asks

Use the Until loop to calculate the and of integers 1 to 100

Script content:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/99/wKioL1epmOLzdrXHAAAr4mEgEbc969.png "title=" os~0@ FM) 4E7 (9l~te[g3ptj.png "alt=" Wkiol1epmolzdrxhaaar4megebc969.png "/>

Script Execution Results:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/99/wKioL1epmOyiIdVSAAANnd5wEeY538.png "title=" 4$ 7v0dg5tu~bhjmk~}t0rbf.png "alt=" Wkiol1epmoyiidvsaaannd5weey538.png "/>


Another format for the for loop (similar to the For loop in our C language)

Syntax format:

For (give the loop control variable an initial value; give it an exit value; Modify the loop control variable));d o

Loop body

Done

Exercise: Write a script that asks

Use another format for the for loop to calculate all the even numbers in 1-100 and

Script content:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/9A/wKiom1epmczRWU1wAAAnHhXvUaM852.png "title=" @P_ Cdcwnpx@vni2@0k8]j85.png "alt=" Wkiom1epmczrwu1waaanhhxvuam852.png "/>

Script Execution Results:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/99/wKioL1epmdaw9_k-AAAN9IDhUvE307.png "title=" Ebn{g " 8HDQ ' 9]{%{~c9rh]e.png "alt=" Wkiol1epmdaw9_k-aaan9idhuve307.png "/>


ping command

The-c #选项可以指定ping的次数 of the ping command, which has been ping down by default

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/9A/wKiom1epmlyDjgFzAABxbkeuRK4456.png "title=" MQW " Fppm@@0f@_34%o_) $LF. png "alt=" Wkiom1epmlydjgfzaabxbkeurk4456.png "/>

The-W # (in seconds) option of the ping command can specify the timeout length, and when we ping a host, if the host is not on the line, the response time will be very long and we can specify how long it waits.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/9A/wKiom1epmxKhe3FDAABFIhO_lLU887.png "title=" {a{uq ' L%K2W (BSOB) jc0sqb.png "alt=" Wkiom1epmxkhe3fdaabfiho_llu887.png "/>

When we ping a host, the ping command returns a value of 0 and vice versa is 1

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/99/wKioL1epm1mC_GZeAACRqs1oR9Y951.png "title=" 13p{] O0akqlqecud (w[5xtx.png "alt=" Wkiol1epm1mc_gzeaacrqs1or9y951.png "/>


awk Command

Awk is a report generation tool and a text processing tool, and its advantage is that unlike the cut command, we can omit the number of delimiters between fields when using the awk command to cut the field, and awk, like SED, reads the text one line at a time when the text is being processed. The line content is then cut internally according to the delimiter specified by the user, but the user-specified delimiter must be an input delimiter, meaning that the specified delimiter must be a delimiter in the contents of that line, and we cut the field by such a delimiter, and the default output delimiter for the awk command is a space. After cutting the fields, awk gives each of the fields that are cut an identifier, the first field is $ $, and the second is a $,... And so on, then the next line of content.

The basic syntax format used by the awk command:

Awk-f (input delimiter), specify input delimiter ' pattern{action} ' filename

Patterns are pattern-matching patterns, which indicate that only rows that match the matching criteria are processed, and if the conditions are met, then the action represents a processing action on those rows that meet the criteria, and if you want to display multiple different fields, simply add a comma between the identifiers of the different fields. The simplest action (processing action) is print (print)

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/9A/wKioL1epoZLieIy-AAA_k1pKLvI566.png "title="%oe9x4 ' L4o1 ' ef7]7vshub5.png "alt=" Wkiol1epozlieiy-aaa_k1pklvi566.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/9A/wKiom1epobeiPm5kAABZfewe0qE670.png "title="%2_ Sd4z2jrc7gw2b~h7qhil.png "alt=" Wkiom1epobeipm5kaabzfewe0qe670.png "/>

Temperature: [ABCD ...]: brackets denote file name wildcard, implementation matches a single character within a specified range

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/9A/wKiom1epn5TiHSS6AAAOA9kEEmw642.png "title=" N ' 3 ' 6Y43]) 2[76]9a3l%psc.png "alt=" Wkiom1epn5tihss6aaaoa9keemw642.png "/>

2> represents the redirect error output in IO redirection

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/9A/wKiom1epoGzxVoggAAAkqianQuE543.png "title=" 5R ( Tkivvqre4@{gnr68xks2.png "alt=" Wkiom1epogzxvoggaaakqianque543.png "/>

&> indicates redirection of all outputs (both correct and incorrect) in IO redirection

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/9A/wKioL1epoO7S7tJWAABE_cRk8tU745.png "title=" 6RYSZ} U0e7epc1_d{e$lvyc.png "alt=" Wkiol1epoo7s7tjwaabe_crk8tu745.png "/>


Exercise: Write a script to create a partition for the specified disk and format it

Requirements

1, list all the disks on the current system, let the user choose, and support the Q exit script, if the user chooses the error, let the user choose again;

2, when the user chooses, remind the user next action may damage the data, and ask the user to confirm the operation, if the user chooses Y to start the partition format, if the user chooses N to exit the script, otherwise let the user choose again;

3, erases all partitions on the specified disk, creates three primary partitions for it, the first partition is 20M, the second partition is 512M, the third partition is 128M, and the third partition is the swap type

Script content:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/9B/wKiom1eprazBILbmAAEBBbJP7bk996.png "title=" h{$}@) L[l{bb3%jag48ry6y.png "alt=" Wkiom1eprazbilbmaaebbbjp7bk996.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/9A/wKioL1eprciD9SlWAACBh94xqDo362.png "title=" j3inw6h) sgp3pu5@4i$qlv4.png "alt=" Wkiol1eprcid9slwaacbh94xqdo362.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/9A/wKioL1epreTDOb9kAABQ0YptD9o802.png "title=" _ S7282gop$_$r]vi ' n~2w ' 9.png "alt=" Wkiol1epretdob9kaabq0yptd9o802.png "/>

Script Execution Results:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/9A/wKioL1epre-iy43lAABrVxIHj2I729.png "title=" [(T} 3V2A) y6%v%~78ig}hja.png "alt=" Wkiol1epre-iy43laabrvxihj2i729.png "/>

• Summarize the above script:

The command to erase all partitions is DD If=/dev/zero of=/dev/sd# bs=512 count=1, overwriting the first 512 bytes of data on a disk, and this part of the data is our mbr,mbr corruption, It's the equivalent of erasing all the partitions.

The->sync command means that the in-memory operation is synchronized to disk, because the command to erase the MBR above is too fast, to prevent the system from synchronizing the in-memory changes to our hard drive, we have to manually synchronize the script and wait for 3 seconds with the Sleep 3 command, Let our system recognize our commands.

The read command continues only after waiting for our user to enter content.

--We use the script to partition the disk, we can directly use the echo command, we want to perform the action to pass to the fdisk/dev/sd# command can be


Temp: 1,fuser-km command, which is used to kick all processes that are using the mount point

2, our grep command, by default, will output the matching line to the display

The Grep-o option, however, is to output only the matched string to the display


This article is from the "Novice Technical Documents" blog, please be sure to keep this source http://zhubo.blog.51cto.com/11395641/1836245

Shell script for disk partition formatting

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.