Shell Script Programming Advanced

Source: Internet
Author: User

Preface: Before we get to the point, let's review the conditional tests on scripting

One, Process Control

  1. Over-programming languages

    A. Sequential execution

    B. Select execution

    C. Cyclic execution

  2. Sequential execution

    As the name implies, it is executed in the order of the content you hit in your script.

  3. Select Execute

    Fi

    A. Conditional Select if statement, can be nested

    B. Format

    Single Branch

    if judgment condition; then

    Condition is true

    Fi

    Dual Branch

    if judgment condition; then

    Condition is true

    Else

    Condition is False

    Fi

    Multi-Branch

    if judgment condition; then

    Condition is true

    Elif (equivalent to else if) to determine the condition; then

    Condition is true

    Else

    All of the above conditions are false

    Fi

    The first time a true condition is encountered, the branch is executed and fi ends the entire if statement.

    Take a look at a few examples to find out exactly what the format is in the script.

    After all the scripts have been written, the actions are the same, and they are shown here once.

    Case

    A.case supports GLOB-style wildcard characters

    * Any character of any length
    ? Any single character
    [ ] Any single character within the specified range
    A|b A or B

    B. Format

    Case variable reference in

    PAT1) Branch;;

    PAT2) Branch;;

    ...

    *) Branch;;

    Esac

  4. Loop execution

    Definition: run a code segment repeatedly, with entry conditions and exit criteria

    For

    A. Format

    For variable name in assignment list;

    Loop body

    Done

    B. Execution mechanism: assigns the element of the list to the variable name sequentially, executes the loop body once each assignment, knows that the list is exhausted, and the loop ends

    C. How the list is generated

    (1) The list is given directly.

    (2) List of integers

    (a) {1..10}

    (b) $ (SEQ 1 10)

    (3) return list of commands $ (cmd)

    (4) variable reference [email protected],$#


    While

    A. Format

    While loop control condition; do

    Loop body

    Done


    B. The loop control condition is true, enter the loop, otherwise exit the loop

    c. Special usage Traverse each line of a file

    While Read Line;do

    Loop body

    Done < filename

    Read each line of the file sequentially, and assign the value to $line


    Until

    A. Format

    until cyclic control conditions; do

    Loop body

    Done

    B. loop control condition is false, enter loop, otherwise exit loop


    Continue

    Used in the loop body, to end the first n layer of this round cycle, into the next round of n+1 layer cycle

    Break

    Used in the loop body, to end the N-level round cycle in advance

    The two are applied in other loop statements, and in the pictures of the other examples.


    Shift

    Shift N

    Used for parameter list, left shift specified number of times, not specified as left shift, once moved, left-most parameter removed from list


    Select used to create menus

    A. Format

    Select variable in List;do

    Circular Body Command

    Done

    B.PS3 prompt

    C. Enter a number in the menu, execute the corresponding command, is the wireless loop, you need to break out of the loop or exit to terminate the script



  5. Special usage

    ((...)) Can be used for operations

    for (variable initialization; conditional judgment expression; control variable's correction expression);d O

    Loop body

    Done

    At the end of each cycle, the control variable correction operation is performed before the condition is judged.


Two, the function

  1. function Introduction

    Not a separate process, cannot run independently, is part of a shell program, runs in the current shell

  2. Defining functions

    A. Mode one

    Function name () {function body;} Common

    B. Mode II

    function function name {functional body;}

    C. Mode three

    Function name () {functional body;}

  3. function uses

    A. Functions can be defined in an interactive environment

    B. You can put a function in a script file as part of it

    C. Can be placed in a separate file containing only functions

    D. function is only executed if it is called

  4. function return value

    Return returned from the function, with the last state command to determine the return value, is 0, is no error return, is 1-255, there is an error to return

  5. Defining and using Functions

    A. Interactive environment

    After the function is defined, it remains until the user exits from the system, or executes the unset delete function, canceling the function assignment

    B. In the script

    Calling functions directly invokes function names


  6. Load function

    After the function file is created, use the. or source to the file name loading function

  7. Execute Shell function

    Directly enter the function name

  8. function parameters

    Can accept calls to $1,$2,... These parameters, as well as special variables

    [Email protected],$* means all the parameters

    $# indicates the number of arguments

  9. function variables

    A. Environment variables

    Current shell and child shell are valid

    B. Local variables

    Valid only during the current function run

    C. Global functions

    Valid for the current shell

  10. function recursion

    function calls itself directly or indirectly



Three, array

  1. Meaning of the related array

    A. Variables

    Storing the memory space of a single element

    B. Arrays

    A contiguous memory space that stores multiple elements, equivalent to a collection of multiple variables

    C. Index

    Starting from 0, numeric index,declare-a array name declaration numeric index

    You can also customize, for associative indexes,declare-a array names to declare associative indexes

  2. Array Assignment

    A. Assigning one element at a time

    Title[1]=a

    B. Assigning all elements at once

    Title= (a b C) subscript automatically starts from 0

    C. Specifying the number of subscripts

    title= ([0]=a [3]=b)

    D. Interactive assignment

    Read-a array Name


  3. referencing arrays

    A. Referencing an array element

    ${array name [subscript number]}

    B. Referencing all elements of an array

    ${array name [@|*]}

    C. Number of array elements

    ${#数组名 [@|*]}

    C. Deleting an array or element

    unset array name [subscript value]

  4. Array Data processing

    A. Referencing elements in an array

    ${array name [@]: I:j}

    I: A positive number is skipped from left to right when I element is removed from right to left

    J: The J element is removed from left to right when positive, and J elements are removed from right to left for negative numbers.



    B. Append elements

    Array name [${#数组名 [@]}]= element value

    C. Associative arrays

    Associative arrays must first be declared and then called

    D. Display string length

    ${#数组名}


Four, advanced string manipulation

    1. Take a string based on a pattern

      ${array name #* string}    Delete from left to right until all characters matching to string first appear

      ${array name ##* string}    From left to right, delete all characters matching to a string until the last occurrence

      ${array name% string *}    Same as # usage, but starts right to left


    2. li>

      find replace

      ${array name/matched string/replacement string}  first match to string, replace

      ${array name//Match string/replacement string}    all matched strings, replace

      ${array name/#匹配的字符串/replacement string}    line header matched string, replace

      ${array name/% Match string/replacement string}    end of line matched string, replace


    3. find delete

      As with replacement usage, Do not enter a replacement string

    4. ${array name ^^}    Converts all lowercase letters in an array to uppercase

      ${array name,,}    Replace all uppercase letters in the array with lowercase


Five, advanced variables

    1. Declare

      Variable with type

      If the variable name is not followed by declare-a and declare-a, it indicates that the index array and the associative array are displayed

    2. Eval

      Scans all permutations first, then executes commands, applies to variables that cannot be implemented at one scan, and two scans of variables.


    3. Mktemp

      Create and display temporary files to avoid conflicts


    4. Install

      Install copy files


    5. Expect

      A. Introduction

      A scenario used primarily for automating interactive operations, which is to automate the interactive operation






Shell Script Programming Advanced

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.