Shell script learning notes (4)-Process Control

Source: Internet
Author: User

Process Control


In Bash Shell, there are two main types of Flow Control commands: Selection and loop. The options include: if, case; for, while, and until. The select command is both select and loop. Either way, a conditional test is required, and then the direction of the program flow is determined based on the test results.

Bash Shell Script can be composed of many commands. After each command is executed, an end state value is returned, and 0 is returned successfully. Otherwise, a non-0 Bash built-in variable $? Stores the status values returned after each command is executed.

If condition judgment

The complete syntax structure is

If condition test 1; then
Command Area 1
Elif condition test 2; then
Command area 2
Else
Command Area 3
Fi


Elif can have no limit on the number of multiple rows. else can have only one row or no

Conditional testing can take multiple forms. If the test result is true or false, check whether the returned value is 0. There are 10 methods for writing conditional testing.
1. Result of executing a command: it can contain the pipeline command, and its result status is the result of executing the last command.
2. Return the opposite value of a command execution result :! Command
3. Use the composite command: (formula). If the calculation result is 0, false is returned. If the value is not 0, true is returned.
4. A substatement consisting of the Bash keyword '[[', ']': [implicit] returns 0 as true, and a non-0 value as false.
5. Use the built-in command to return 0 to the true value of the test criterion, and 1 to escape the false value.
6. Use the built-in command: The [Statement type] is the same as that of test.
7 use-a and-o for logical combination a and o or
8. Command 1 & command 2
9 command 1 | command 2
10 & | Reuse

Limit type of file attributes

String-Based Conditional Criterion

About the conditional formula of formula

Case

Case items to be tested in
Style Serial 1) Command Area 1 ;;
Style Serial 2) Command Area 2 ;;
Style serial 3) Command Area 3 ;;
...
*) Command area ;;
Esac

When there are several strings in a style string, use | to separate them

Style serial can be written as '(style serial)' or 'style serial) ', which is more common.

 

For

For variable in serial

Do

Command Area

Done

 

Serial is a combination of strings, which are separated by separated characters (such as space characters) defined by $ IFS. These strings are called fields.

 

In serial
Can be left empty, equivalent

For variable in $ @

Do

Command Area

Done

$ @ Indicates all parameters in the command line.

 

Another for format

For (initial condition; termination condition; change item ))
Do
Command Area
Done

 

While

While condition test
Do
Command Area
Done

 

Until

Until condition test
Do
Command Area
Done

 

Select command

Select option variable in serial
Do
Command Area
Done

 

Select command to create a list. List options, that is, each field in the serial. The list prompt symbol is defined by the built-in variable PS3 of Bash. The default value is #?. You can customize the hint as long as you change the value of the PS3. Select will give each option a number, increasing from 1. When you enter a number and select an option, the content of the option is set to the option variable, and the number value is entered in the REPLY variable.

To end the select list, enter the break command in the command area or press ctrl-D to end.

 

Break and continue

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.