My shell script programming

Source: Internet
Author: User

For Shell scripting features it is

Programming languages, script type languages, interpreted languages.

There are always three execution structures to follow when writing scripts: sequential execution of structures, selection of execution structures, and loop execution structures.

Sequential execution: All statements (commands) are executed from left to right, top to bottom

Select the execution structure: according to the logical judgment result of the given condition or according to the optional range of values, then select the statement in a branch to execute;

If: Branch selection criteria; logical judgment results;

Case: Branch selection criteria, according to optional values;

Loop execution structure: For a particular statement, repeat 0 times, 1 or more times;

Has for: Iterates through the specified list

While: results based on logical judgments

Until: According to the result of logical judgment

Select: Dead loop, using a looping mechanism to provide a selection list

----------------------------------------------------------

If structures typically have two-branch and multi-branch structures

is to add elif under if, for example

if CONDITION1; Then

STATEMENT

...

Elif CONDITION2; Then

STATEMENT

...

Elif CONDITION3; Then

STATEMENT

...

...

Else

STATEMENT

...

Fi

Case statements are used to make different results when giving different parameters

Case variable reference in

Mode 1)

Branch 1

;;

Touch Type 2)

Branch 2

;;

...

*)

Default Branch

;;

Esac

There are many ways to match patterns here.

1. Normal text characters

2.globbing-style wildcard characters

*: Any character of any length

? : Any single character

[]: Any single character within the range

[^]: Any single character outside the range

3.| : OR

The For Loop has two forms

1. Traversing the list

For Vaar_name in LIST; Do

Loop body

Done

This list of lists can:

1): Write directly

2): List of pure integers

SEQ: Output An integer list

seq [First [INCREMENT]] Last

3): curly braces unfold

{first.. Last}

{0..100..3} shows numbers divisible by 3 in 0-100

4): Command execution result return value

5): GLOBBING (wildcard character)

6): References to certain variables: [Email protected],$*

2. Format of control variables

for (expression 1; expression 2; expression 3); Do

(Loop body))

Done

Expression 1: Assigning an initial value to a variable

Expression 2: Exit criteria for loops

Expression 3: Transformation Law of variable values

While, until loops:

While CONDITION; Do

Loop body

Done

Conditions to enter the cycle: Conditon has always been true;

Exit loop Condition: Conditon is False

-------------------

Until CONDITION; Do

Loop body

Done

Conditions to enter the cycle: Conditon has been false;

Exit loop Condition: Conditon is True

Note: For the while and until two loop structure, if you want to implement the variable increment operation, it must be given manually;

For example: using the while and until loop structure, calculate all integers within 100 and;

#!/bin/bash

#

Declare-i I=1

While [$I-le 100]; Do

Let sum+= $I

Let i++

Done

There is also an infinite loop of usage for loop statements

While true;

Loop body

Done


until false; do

Loop body

Done


In such a circular structure, continue and break must be used appropriately to ensure that the cycle does not persist;

Continue

Continue [n]

Advance technology nth layer of this cycle, directly into the next round of conditions to determine, if the conditions of the circulation, the next round to open the cycle;

Break

Break [n]

End the N-tier cycle in advance, and no further loops;

There is also a while, until loop that implements the traversal function for each row of the file read operation

while read LINES; Do

Loop body

Done </panth/from/somefile

Until! Read LINES; Do

Loop body

Done </path/from/somefile


Select

The Select loop is used primarily to create a menu-style list for users to select;

The list is arranged in numerical order, we just select the number;


In general, select is used with case;


Select is an infinite loop structure, so you must use the break command in the loop body to exit the loop, or you can use the Exit command to terminate the script directly.


Select NAME [in Word ...;] do command; Done


Select naml6e [in Lsit]; Do

Command

Done

For interactive commands with the user read

Read command:

Read [-a array] [-P prompt] [-t timeout] [name ...]

The name is usually the variable name or array name, and if the name is not written, the system will save read read information in the reply variable;


Note: When using the Read command, the time-out is usually specified with the-t option, and once the time-out is defined with the-t option, we must later determine if the given variable is empty, and if NULL is required to provide a default value for the variable;


Take a random number of 8 or less

echo $[$RANDOM%8]



This article from "12445165" blog, declined reprint!

My shell script programming

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.