Shell Programming Scripting Syntax

Source: Internet
Author: User
Tags case statement

Learning two months of Linux, remember a lot of commands, know the role of the script, also by the script killed about half of the brain cells, and now the script is not skilled use, feel lost. Love my brain cells, hate the script, but have to say that the script is a good thing ah, it is really convenient to use, but write it really burn the head! The following summary of this week to learn the scripting syntax, wow, although not many grammar is not difficult, but the combination of skilled use has a certain degree of difficulty, not to mention now the script only a few lines, after a few lines, the heart no points! Talk less, start

Skip the most basic command-line stacked scripts and summarize the syntax to make the script more concise and practical

First, conditional selection if statement comes up

If statement usage: Common single-branch, dual-branch, multi-branch.

Can the actual use of a single branch statement can not be in the mind no points, direct multi-branch, will be multi-branch, single-and dual-branch naturally will be

if judgment condition 1; then

Command code with true condition

Elif Judging Condition 2;then

Command code with true condition

Elif Judging Condition 3;then

Command code with true condition

Else

The above conditions are false command codes


As an example:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/06/B8/wKiom1m8336A3cyoAAA59FLxfig785.png "title=" Qq20170916154112.png "alt=" Wkiom1m8336a3cyoaaa59flxfig785.png "/>

Complete

Conditional Judgment: Case statement

Case $ in

Condition 1)

Branch 3

;;

Condition 2)

Branch 2

;;

......

*)

Default Branch

;;

Esac

Case supports GLOB-style wildcard characters:

*: Any length of any character "such as search for files, k*) matches all K-beginning filenames"

?: any single character

[]: Any single character within the specified range

A|b:a or B

As an example:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/06/B8/wKiom1m835Hx48m_AABMRxKRINM031.png "title=" 22. PNG "alt=" Wkiom1m835hx48m_aabmrxkrinm031.png "/>

Loop statement for while until shift select

function as its name, repeatedly running a code segment multiple times

For loop usage

For variable name in list;

Loop body

Done

Execution mechanism: Assigns the element in the list to the "variable name" in turn; The loop body is executed once each assignment; Until the elements in the list are exhausted, the loop ends

"General routines: variable names are typically variables defined above, which are used to reference such as: $i or positional variables such as: $1,$2

Lists are typically generated by invoking command results such as ' SEQ 1 ' $ (ls/etc) "

The For Loop also has a special format: similar to the C language

For (control variable initialization; conditional judgment expression; control variable correction expression))

(i=0; i<=100; i++))

Do

Loop body

Done

Give two examples: (a normal for loop, a for loop special format)

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A5/68/wKioL1m833Sgzj9-AABZgHcl11g241.png "title=" 333. PNG "alt=" Wkiol1m833sgzj9-aabzghcl11g241.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/A5/68/wKioL1m84w6x5z20AAAkc550O7Y487.png "title=" 444. PNG "alt=" Wkiol1m84w6x5z20aaakc550o7y487.png "/>

While Loop statement:

Usage:

While Condition;do

Loop body

Done

CONDITION: cyclic control conditions; Before entering the cycle, make a judgment; once each loop is judged again; the condition is "true", then a loop is executed, until the condition test state is "false" terminates the loop so: Condtion generally should have a loop control variable , and the value of this variable is constantly being corrected in the loop body

Here you can create an infinite loop

While True;do

Loop body

Done

The For Loop has a special format, while also has

Special usage of the while loop (traversing each line of the file):

while read line; Do

Loop body

Done </path/from/somefile

Read each line in the/path/from/somefile file sequentially and assign the row to the variable line

The introduction of the usage is complete, for example:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/B8/wKiom1m85lDw0iYbAAAyLHfzVaU358.png "title=" 555. PNG "alt=" Wkiom1m85ldw0iybaaaylhfzvau358.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/B9/wKiom1m87SvAGP_eAAA8QIQAXz4874.png "title=" 666. PNG "alt=" wkiom1m87svagp_eaaa8qiqaxz4874.png "/>select Loop statement

Usage:

The select variable name in list is similar to the for statement, as are the routines.

Do

Circular Body Command

Done

The Select loop is used primarily to create menus, and menu items in numerical order are displayed on standard errors, and the PS3 prompt is displayed.

Wait for user input, user input is saved in built-in variable REPLY

Select often uses the Build menu list with Case Federation. [Not a meal menu!!] ]

Select is an infinite loop, so remember to exit the loop with the break command, or terminate the script with the Exit command. You can also press CTRL + C to exit the loop

Example: 650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A5/69/wKioL1m88KWiZt8MAABoMVL-y1Q595.png "title=" 7777.png "alt=" Wkiol1m88kwizt8maabomvl-y1q595.png "/> loop control shift command;

Used to move the list of parameters to the left for a specified number of times, the default is to move left once.

When a while statement is used, and the while loop iterates through the list of positional parameters, it is often used to shift

Example:

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/B9/wKiom1m89GSQUp5lAAAYXlLZnLs535.png "title=" 88. PNG "alt=" wkiom1m89gsqup5laaayxllznls535.png "/> Execution Result: (believe that you know everything)

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A5/69/wKioL1m89FHR3lnMAAArUD8lCS4739.png "title=" 99. PNG "alt=" Wkiol1m89fhr3lnmaaarud8lcs4739.png "/>

Jump out of a single loop Continu statement:

Continue [n]: End of the nth layer of the current cycle, and directly into the next round of judgment; the inner layer is the 1th floor.

Come on, let's take an example:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/B9/wKiom1m89gyxQakOAAAu5ocTL-M275.png "title=" Qq20170916175832.png "alt=" wkiom1m89gyxqakoaaau5octl-m275.png "/> jumps out of the entire loop break statement:

Break [n]: End the nth cycle prematurely, the inner layer is the 1th layer "notice is the end of the nth cycle, not the current cycle"

Another example:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/06/B9/wKiom1m89yaDEdMCAAAy5qumw9k029.png "title=" 111. PNG "alt=" Wkiom1m89yadedmcaaay5qumw9k029.png "/>

Here the Shell programming script base statement is almost finished, and then you can call the function, array in the script. In this way, the use and writing of scripts can be more simple and clear, the next blog to introduce the definition and invocation of functions, arrays of definitions and calls, I'm going to write a script, bye~~


Shell Programming Scripting Syntax

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.