How to use the Loop statement of shell script programming
Editor to share with you how to use the circular sentence of shell scripting programming, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
It is common to repeat a series of commands while running a script, and we need to use loop statements to do this.
1. For command
Format: for variable in list; do
Cyclic body
Done
The for command iterates through each value in the list and exits the loop after the traversal is complete.
The list has the following forms:
1. A series of values defined in the command
2. Variables
3. Command
4. Catalogue
5. Documents.
Example 1:
In the example above, {initial value is used. The ending value} format to represent the numerical value of an interval.
Example 2: use variables as a list:
Example 3: generate a list with the command:
In the above example, the seq command is used to generate the list. The format of the seq command is: seq [initial value step] termination value.
Using backquotes in the above two examples will get the result of the command running.
Example 4: use the directory as a list:
II. For commands in C language style
Format: for ((variable assignment; Loop termination condition; step size); do
Cyclic body
Done
Example: add the sum from 1 to 100:
3. Until command
Format: until condition; do
Statement
...
Done
The loop exits when the conditions in the until command are met.
Example:
4. While command
Format: while condition; do
Statement
...
Done
The while command enters a loop when the condition is met.
Example:
V. Control cycle
1. Break command
Function: exit a loop in progress.
Example: exit the loop if the variable I is 3.
2. Continue command
Function: enter the next cycle ahead of time.
Example: add the numbers between 1 to 20 and 51 to 100:
VI. Special usage of while
1. Endless cycle
Format: while:; do
Statement
Done
Example:
2. Read the contents of the file line by line into the loop
Format: while read variable name; do
Cyclic body
Done < file
Example: read the contents of / etc/passwd file line by line:
The above is all the contents of this article entitled "how to use cyclic statements in shell scripting programming". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!