Discussion on bash loops and branches

Source: Internet
Author: User

First, Cycle

1.for Loop

The For loop in the bash script, edit bash file 1.sh, enter the following code:

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

Run 1.sh, that is, enter bash 1.sh in the terminal, and the results are as follows:

Execution result Analysis: The first for loop format does not add "" to the following number, makes these numbers not a whole, but the second for loop format adds "" To the following data, which means that the following numbers are considered as a whole, so the output of the two for loop output is different ...


2.while Loop

The while loop is as follows: Edit Bash file 2.sh, in which you enter the following code: (output 0 to 10 values)

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


The results of running 2.sh files are as follows:


3.until Loop

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

Code Analysis: Here is the end assignment, with the until loop, using the read inner to enter a value, exiting the program when the value entered is the same as the end in the until code.

The results of the operation are as follows: (the part of the red line is the contents 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 the following: Edit the a.sh file and enter the following code: (Nested output inner loop and outer loop)

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

The output results are as follows:


third, cycle control

1.break

A break is a termination loop, as shown in the following example: (Output less than 6 value)

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


The results of the operation are:


2.continue

The continue statement represents skipping over the loop body, as follows: (output from 1 to 10 of values other than 3 and 6)

Code Analysis: Here the first is to use the while loop statement, which guarantees that the value is between 1 and 10, and there is an if statement after the while statement, which means that when A's value equals 3 or equals 6 o'clock, it jumps 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 action is equivalent to switch in C language role, specific examples are as follows:

Code Analysis: Input keypress, where case "KeyPress" in is format, remember to add Esac before exit 0 ... It's all in the case format ...


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


2.select Statement

A SELECT statement can create a menu, as follows: (choose your favorite vegetables)

Code Analysis: The format of the SELECT statement is as above, spaces are required between words in the following ... The beginning of the prompt statement needs to use PS3 as the amount, and the other string will not appear. Or you can directly change the whole sentence to echo "Choose your favorite vegetable:" There is a point must be noted in the done before must be added break, otherwise the program will always be implemented, can not terminate ...


The results of the implementation are as follows:


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.