Shell programming Control structure: expr, let, for, while, until, shift, if, case, break, continue, function, select

Source: Internet
Author: User
Tags case statement new set

1. expr computes the integer variable value

S= ' Expr 2 + 3 '

The arithmetic symbol and the parameter must have the space separate;

2. Let command

let s= (2+3)

echo $s

3, for statement

For variable in list do command line (typically used for loop variables) Done
#!/bin/bashfor var in one, three four five        do                echo------                echo ' $var are ' $var        done

Returns a value as a command list #!/bin/bashfor var in ' ls ' does                echo-----                echo        $var done

4. While statement

Syntax format: while
expression do command-line done
#!/bin/bashnum=1while [$num-le]doecho-e "\ t the num is $num" let Num=num+1done

5. Until statement

Syntax format:
unitil expression do command-line done
#!/bin/bashsum=0num=10until test $num-eq 0        do                sum= ' expr $sum + $num '                num= ' expr $num-1 '        doneecho "sum = $sum "

6, Shift statement
Shift Statement: Pass the value of a variable to the left and form a new set of parameter valuesExample: Position variable current value is: 1=file1 2= file2 3=file3 Execute once S Hift: 1=file2 2=file3 You can also specify the number of times the position variable is transferred in the shift command - shift n
#!/bin/bashwhile [-N "$*"] do                echo $ $4 $ $6                shift        Done

7. If statement

The general form of the if statement:
if   conditional expression then  #当条件为真时执行以下语句   &NB sp;      command List else #当条 Execute the following statement when the piece is False      & nbsp;   command List fi
#!/bin/bashif test-f "then echo" $ is an        ordinary file "else echo" $ not a        ordinary file "fi

8. Case statement

   the value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be either a variable or a constant. The value detects each pattern that matches, and once the pattern is matched, all commands begin to execute until;;. Other modes are no longer resumed after the corresponding command is executed in match mode. If there is no matching pattern, use the * number to capture the value   [note] 1. Wildcard characters 2 can be used in a pattern string. If a pattern string contains more than one pattern, then the patterns should be in a vertical bar (|) , the table mode is a "or" relationship, that is, whenever a given string matches one of the patterns, a subsequent list of commands is executed. 3. Each pattern string should be unique and should not be repeated, and the order of their appearance should be reasonably arranged, for example, "*" should not be used as the first pattern string, because "*" can match any string, and if the first one appears, no more patterns will be checked. 4. The case statement begins with the keyword case and ends with the keyword ESAC. 5. The exit (return) value of the case is the exit value of the last command executed in the entire structure. If no command is executed, the exit value is 0.
#!/bin/bashcase $ in1)        echo "You choice is 1";; 2)        echo "Your choice is 2";; *)        echo "Your choice is others";; Esac

9. Break and Continue

1, break: Used to immediately terminate the execution of the current loop, and the break command allows the user to exit from the loop body. syntax: Break[n], where n means to jump out of several layers of loops, the default value is 12, continue: Skips the statement after it in the loop body, returns to the beginning of the loop layer, Proceed to the next cycle. syntax: Continue[n], where n means jumping from the most inner loop body containing the continue statement to the first layer loop, the default value is 1, and the number of loop layers is numbered from the inside out.

10. Functions

function: Consists of a function title and a function body. The title is the function name. A function body is a collection of commands within a function. The title name must be unique. Variables are global variables, no local variables.

#!/bin/bashnum=1hello () {        echo "Hello boy~ It ' s our $num meeting" let        num=num+1}

11. SELECT statement

Format:

Select variable in list do command line (usually used to loop variable) done

Make a selection table and select an option in the list to execute the command line. If the selected variable is not in the list sequence, a null value is returned. You need to exit the loop with break.

#!/bin/bashecho "A is 5, and B is 3." Please select your method: "A=5b=3select var in" A+b "" A-B "" A*b "" A/A "do        breakdonecase $var in" a+b ")        Echo ' a+b= ' ' Expr $a + $b ';; " A-B ")        echo ' a-b= ' expr $a-$b ';;" A*b ")        echo ' a*b= ' expr $a \* $b ';; A/b ")        echo ' a/b= ' expr $a/$b ';; *)        echo "Input error" Esac



Shell programming Control structure: expr, let, for, while, until, shift, if, case, break, continue, function, select

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.