In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
In addition to while loops and until loops, Shell scripts also provide for loops, which are more flexible and straightforward to use. The Shell for loop can be used in two ways, which we'll explain one by one.
For cycle of C language style
The use of C-style for loops is as follows:
For ((exp1; exp2; exp3))
Do
Statements
Done
The following points are explained:
Exp1, exp2 and exp3 are three expressions, in which exp2 is the judgment condition, and the for loop decides whether to continue the next loop according to the result of exp2.
Statements is a circular body statement, which can have one or multiple statements.
Do and done are keywords in Shell.
Its operation process is as follows:
1) execute exp1 first.
2) execute exp2 again, and if its judgment is true, execute the statement in the body of the loop, otherwise end the entire for loop.
3) execute the exp3 after executing the loop body.
4) repeat steps 2) and 3) until the judgment of exp2 is not valid, the cycle is ended.
In the above steps, 2) and 3) merge together as a loop, which will be repeated, and the main function of the for statement is to continue to execute steps 2) and 3).
Exp1 is executed only on the first loop and will never be executed again, which can be thought of as an initialization statement. Exp2 is generally a relational expression that determines whether to continue with the next loop, called a "loop condition." In many cases, exp3 is an expression with self-increment or self-subtraction, so that the loop condition gradually becomes "untenable".
The execution of the for loop can be shown in the following figure:
Let's give a practical example to calculate the sum from 1 to 100.
#! / bin/bash
Sum=0
For ((iTun1; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.