On bash cycle and branch

Source: Internet
Author: User

First, the circulation

1.for Cycle

For loop in bash script, edit bash file 1.sh, enter code as follows:

(The code in the example is intended to output numbers from 1 to 10)

Running 1.sh, that is, entering bash 1.sh at the terminal, the results are as follows:

Execution result Analysis: The first for loop format does not give the following number "", so that these numbers are not a whole, but the second for loop format to the back of the data added "", that is, the back of the number is considered a whole, so the output of two for the loop output is different!!!


2.while Cycle

The While loop example is as follows: Edit bash file 2.sh, where you enter the following code: (output 0 to 10 values)

Code Analysis: While loop here to determine whether the value of var0 is less than the value of the range limit, if less than the value of the output var0, echo-n meaning is not newline; var0=$ ((Var0+1)) represents VAR0 plus 1.


The results from running the 2.sh file are as follows:


3.until Cycle

An example of the until loop is as follows: Edit the 3.sh file and enter the following code: (Exit the program when the input instruction is the specified instruction)

Code Analysis: This is the end assigned to end, with the until loop, using the read inner input value, when the input value and the until code in the same time, the program exits.

The result is as follows: (the part of the red line is the content of the keyboard input)



Second, nested loops

The understanding of nested loops is similar to the understanding of programming languages such as C language

A specific example is as follows: Edit the a.sh file and enter the following code: (Nested output inner loop and outer loop)

Code Analysis: First assign a value of outer to 1 and use a For loop to nest a B loop in a loop.

The output results are as follows:


Third, Cycle control

1.break

Break is the terminating loop, as shown in the following example: (Output less than 6 value)

Code Analysis: The first is to set the scope, execute a while loop, determine whether a is eligible at this point, and then will a++, determine whether a is greater than 5, if satisfied, then execute the break statement!


The result of the operation is:


2.continue

The continue statement means skipping over to the loop body, as shown in the following example: (Output 1 to 10 in values other than 3 and 6)

Code Analysis: Here is the first use of a while loop statement, the guaranteed value between 1 and 10, after the while statement has an if judgment statement, that is, when the value of a is equal to 3 or is equal to 6 o'clock, jump out of the loop.

The results of the operation are as follows:



Iv. Testing and branching

1.case (in)/ESAC format

Case in bash script is equivalent to the role of switch in the C language, as follows:

Code Analysis: Enter KeyPress, where case "KeyPress" in is the format, remember to add esac!!! in front of exit 0 This is the case format!!!


The results of the operation are as follows: (the part of the red line is the keyboard input)


2.select statements

The SELECT statement can create a menu, with the following examples: (choose your favorite vegetables)

Code Analysis: The format of the SELECT statement as above, in the following words need space between!!! The beginning of the prompt statement needs to use PS3 as the amount, the other string will not be displayed! Or you can directly change an entire sentence to echo "Choose your favorite vegetable:" There is a point to be sure to add a break in front of the done, otherwise the program will always be executed, can not be terminated!!!


The results of the implementation are as follows:


On bash cycle and branch

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.