Shell Process Control of Linux shell learning

Source: Internet
Author: User
Tags case statement

In the Linux shell programming, the process control structure and the statement, also is the shell script the emphasis, does not understand the friend, follows the script small compilation together to study.

Linux control flow structure learning.

One, shell control flow structure

1. Control structure 6.while Cycle
2.if then else statement 7.until Loop
3.case Statement 8.break Control
5.for Loop 9.continue Control

1,if then Else
If condition 1 if condition 1 is true
Then
Command 1 Execute Command 1
Elif Condition 2 If condition 1 is not established and condition 2 is established
Then
Command 2 Execute Command 2
else if the conditions are not true
Command 3 then execute the command 3
Fi complete

2,case statements
The case statement is a multi-select statement. A case statement can be used to match a value with a pattern, and if the match succeeds, the matching command is executed. The case statement is in the following format:
Case value in
Mode 1}
Command 1
. . .
; ;
Mode 2)
Command 2
. . .
;;
Esac

3,for Cycle
The general format for A for loop is:
For variable name in list
Do
Command 1
Command 2?
Done
When the value of the variable is in the list, the For loop executes all commands once, using the variable name to access the value in the list. The command can be any valid shell command and statement. The variable name is any word. The in list usage is optional, if it is not used, the For loop uses the command-line positional parameters. The in list can contain replacements, strings, and file names.

4,while Cycle
The while loop is used to continuously execute a series of commands and to read data from the input file in the form of:
While command
Do
Command 1
Command 2
. . .
Done
Although you typically use only one command, you can place several commands between while and do. Commands are often used as test conditions.
The command between do and done is executed only if the exit status of the command is 0 o'clock, and the loop terminates if the exit status is not 0.
The command finishes, control returns to the top of the loop, and starts from the beginning until the test condition is false.

5,until Cycle
The Until loop executes a series of commands until the condition is true. The Until loop and the while loop are just the opposite of the processing mode.
The general while loop is better than the until loop, but at some point-and only in rare cases-the until loop is more useful.

The Until loop format is:
Until conditions
Command 1
. . .
Done
The condition can be any test condition, and the test takes place at the end of the loop, so the loop executes at least once-note this.

Using break and continue to control loops
The break command allows you to jump out of a loop. Break usually exits a loop or case statement after some processing. If you are in an embedded loop, you can specify the number of loops to jump out.

For example, if you are in a two-tier loop, just jump out of the loop with break 2.
The continue command is similar to the break command, with only a few important differences, and it does not jump out of the loop, just skip this loop step.

Shell Process Control of Linux shell learning

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.