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

Source: Internet
Author: User

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

1. expr calculates the integer variable value

S = 'expr 2 + 3'

The operator numbers and parameters must be separated by spaces;

2. let command

Let s = (2 + 3) * 4

Echo $ s

3. for statement

 

For variable in list Do Command Line (loop variables are usually used) Done
#!/bin/bashfor var in one two three four five        do                echo ------                echo '$var is' $var        done

Use the command return value as the list #! /Bin/bashfor var in 'LS' do echo ----- echo $ var done

4. while statement

Syntax format:
While expression Do Command Line Done
#!/bin/bashnum=1while [ $num -le 10 ]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: transfers the value of a variable to the left in sequence and forms a new set of parameter values.- For example, the current value of the location variable is: 1 = file1 2 = file2 3 = file3- After one shift operation, the value is 1 = file2 2 = file3. You can also specify the number of times the location variable is transferred in the shift command.- Shift n
#!/bin/bashwhile [ -n "$*" ]        do                echo $1 $2 $3 $4 $5 $6                shift        done

7. if statement

The general form of the if statement:
If condition expression Then # execute the following statement when the condition is true Command list Else # execute the following statement when the condition is false Command list Fi
#!/bin/bashif test -f "$1"        then        echo "$1 is an ordinary file"else        echo "$1 is not an ordinary file"fi

8. case statement

The value must be followed by the word in, and each pattern must end with parentheses. The value can be a variable or constant. The value is used to detect each mode that matches. Once the mode matches, all commands are executed ;;. After the matching mode command is executed, other modes are no longer available. If no matching mode exists, use the * sign to capture the value and accept other input. [Note]1. Wildcard characters can be used in a pattern string.2. If a mode string contains multiple modes, the modes should be separated by a vertical line (|). The table modes are in the "or" relationship, that is, as long as the given string matches one of the modes, the subsequent command list will be executed.3. Each pattern string should be unique and should not be repeated, and the order of appearance should be reasonably arranged. For example, "*" should not be used as the first pattern string, because "*" can match any string. if the first one appears, no other mode will be checked.4. The case statement starts with the keyword case and ends with the keyword esac.5. The exit (return) value of 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 $1 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 terminate the execution of the current loop immediately. The break command can cause the user to exit from the loop body.- Syntax: break [n]. Where, n indicates the number of loops to jump out. The default value is 1.- 2. continue: Skip the statements in the loop body and return them to the beginning of the loop layer for the next loop.- Syntax: continue [n]. Where, n indicates to jump from the inmost loop body containing the continue statement to the nth layer of the loop. The default value is 1, and the number of loop layers is from the inner to the outer.

10. Functions

Function: consists of the function title and function body. The title is the function name. The function body is a set of internal functions. The title name must be unique. All variables are global variables without local variables.

 

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

 

11. select statements

Format:

Select variable in list Do Command Line (loop variables are usually used) Done

 

Create 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. Use break to exit the loop.

#!/bin/bashecho "a is 5 ,b is 3. Please select your method: "a=5b=3select var in "a+b" "a-b" "a*b" "a/b"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


 


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.