I. INTRODUCTION
In shell programming, circular commands are used to control the execution of certain statements under certain conditions, and in programming languages, loop statements are one of the most basic syntaxes, and are no exception in the shell. In the shell of the circular statements generally have for, while, until these, and occasionally write the wrong grammar, here combined with examples to summarize their own. It also provides a quick access to information for future use. There are three commonly used loop statements: for, while, and until. While loops and for loops belong to the "loop of the type", while the until belongs to the "until loop".
Two. Detailed
1.for Cycle
For variable name in list;
Loop body
Done
which can be replaced by a carriage return, the same effect
(1) most basic for loop
#!/bin/ for in {1.. 5 };do echo' done '
- The command between do and done is called the loop body, and the number of executions is the same as the number of constants or strings in the list. For loop, first assigns the first constant or string of the in after list to the loop variable, then executes the loop body, which executes the list, and finally executes the command sequence after the done command.
- The shell support list for loop uses a slightly written count, and the range of the ranges is denoted by {1..5} (curly braces cannot be removed or treated as a string). Where numbers can also be expressed using $ (SEQ).
(2) The Sheel also supports the implementation of a list for loop in a specified number of steps, For example, calculate the sum of all the odd numbers within the 1~100.
#!/bin/bashsum=0 for in {1: . 2}; Do sum + = i-done echo "sum= $sum "
by I press 2 constant increment, get 1-100 odd sum. We can also use SEQ to indicate that: for I in $ (SEQ 1 2), SEQ indicates a starting number of 1, the number of steps to skip is 2, to 100 ends.
(3) for can also be used in conjunction with other selection statements , for example: Create a script name useradd.sh, when the-a option is executed, the user Mageuser1~mageuser20 is created in bulk, The UID for the Mageuserx is required for the 200x,mageuserxx UID for the 20xx,1~8 user's shell for the/sbin/nologin,9~20 user's uid is/bin/bash. The last 20 user passwords are set to CentOS, and the user mageuser1~mageuser20 is deleted in bulk when the-D option is executed. Deleted together with the home directory.
#!/bin/Bash Case$1 inch-a) forNinch{1.. -} ; Do if[$n-le8]; ThenUseradd-U $[ -+ $n]-s/sbin/Nologin mageuser$nElseUseradd-U $ (Echo "2000+ $n"|BC) mageuser$nfi Echo "CentOS"|passwd--stdin mageuser$n &>/dev/NULL Echo "Mageuser$n created" Done;; -D) forNinch{1.. -} ; DoUserdel-R mageuser$n Done;; *)Echo "Please type-a or-d";;Esac
Case is a conditional judgment statement, which is divided into-a and-d, preferably using case (note that after each case branch ends, there are two;). echo "CentOS" |passwd--stdin mageuser$n &>/dev/null is a batch change password, throw it into/dev/null, do not execute.
2. While loop
While Condition;do
Loop body
Done
CONDITION: cyclic control condition; Before entering the loop, make a judgment before each cycle, and the loop is "true", then a loop is executed, until the loop terminates the loop as "Flase".
Condition generally should have a cyclic control variable, so the value of the variable is constantly being corrected in the loop body.
The number of while repetitions before entering the loop is a condition that controls whether the statement continues to execute repeatedly. In order to avoid a dead cycle, it is necessary to ensure that the loop body contains the cyclic exit conditions
(1) Basic while loop , for example: printing from 1 to 10
#!/bin/bashn=0 while]; do Echo $n let n+ + done
First define a variable n, determine the value of N and 10 of the size of the relationship, as long as the value of n is true, the loop will continue. Let n++ is to have the value of n add 1 at a time.
(2) Circular control statement continue
Continue used in the loop body
Continue [n]: Early end of the nth layer of this round cycle, directly into the next layer of circulation; The inner layer is the first layer (1 can also be omitted).
1) Continue basic application
#!/bin/bash
N=0
While [$n-lt];d o
Echo $n
Let n++
If [$n-eq 5];then
Continue
Fi
Done
The continue terminates the cycle and continues the next loop, so the print result is still 0-10.
(3) Loop control statement break
Break is used to jump out of loops and can jump out of any type of loop
1) break jumps out of the single layer loop
#!/bin/Basha=1while5 ]; do if 3 ] Then break fiecho "a= $a " a=$[$a +1] Done
This script a starts with a value of 1, a<5, always loop, when A=3, break terminates the loop, so print out a value of 1 and 2.
2) break jumps out of the multilayer loop
Break N,n represents the number of loop layers to jump out, n=1 by default, which means that only the current loop is out.
#!/bin/Basha=1 while[$a-le5]; Do Echo "a= $a"a=$[$a +1] forNinch 1 2 3 4 5; Do if[$n-eq3]; Then Break2 fi Echo "n= $n"N=$[$n +1] Done Done
A For loop is performed in the script, and n=3 terminates the loop, but does not interfere with the loop in the while, so it jumps out of the for loop and prints out the n=1,n=2.
3.until Cycle
Until Condition;do
Loop body
Done
Entry condition: Condition is false
Exit Condition: Condition is true
The until command is similar to the while command, but contrary to the while, the Whie loop resumes the loop when the condition is true and until executes the loop when the condition is false.
#!/bin/bashuntil ]; do Echo $n let n+ + done
The initial value of n is 0, each loop plus 1, because the condition is false when the loop is executed, will print from 0 to 0, when n=10, the condition is true, terminates the loop. So the print result is 0-9.
For,while,until of Shell Loops