Summary of circular statements
This article describes in detail the loop statement of the flow control statement, in turn:
1,for Loop Statement general use and special use
2,while loop statements are generally used with special customs
The 3,until statement uses
4, loop control: Use of continue,break in circular statements
5, use of functions
1,for Loop Statements
the general format for A for loop is : The list matches the variable sequentially, and do is followed by the execution body. The list defaults to one or more spaces or tab intervals
For variable in list
Do
Command1
Command2
...
CommandN
Done
Example: List 1 to 5 numbers
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/52/wKiom1X6LbKySqejAAB-mvRQb3k030.jpg "title=" For2.jpg "alt=" Wkiom1x6lbkysqejaab-mvrqb3k030.jpg "/>
Example: command referencing using commands
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/52/wKiom1X6LqHx8LMNAAB6sCE7Tgo755.jpg "title=" For2.jpg "alt=" wkiom1x6lqhx8lmnaab6sce7tgo755.jpg "/>650" this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/ 73/4f/wkiol1x6mwar3pssaaclw2uca_g449.jpg "title=" for3.jpg "width=" 278 "height=" 296 "border=" 0 "hspace=" 0 "vspace=" 0 " Style= "WIDTH:278PX;HEIGHT:296PX;" alt= "wkiol1x6mwar3pssaaclw2uca_g449.jpg"/>
special usage for the FOR loop: for (assignment; condition; Operation statement)
For ((EXPR1;EXPR2;EXPR3)); Do
Loop body
Done
EXPR1: Defines the control variable and assigns the initial value;
EXPR2: cyclic control conditions;
Entry condition: Control condition is "true"
Exit Condition: Control condition is "false"
EXPR3: Correcting control variables
Example: The sum of all positive integers within 100 is obtained;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/4F/wKioL1X6MffiBDFkAADGXqLr310881.jpg "title=" for special. jpg "alt=" wkiol1x6mffibdfkaadgxqlr310881.jpg "/>
2,while Loop Statements
While loop General format syntax:
While Condtion; Do
Loop body
Done
Entry condition: When condition is "true";
Exit Condition: When condition is "false";
While CONDITION; Do
Loop body
Correction expressions for control variables
Done
Example: The sum of all positive integers within 100 is obtained;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/52/wKiom1X6MTWiX-JPAACgpkfwb0s052.jpg "title=" While1.jpg "alt=" Wkiom1x6mtwix-jpaacgpkfwb0s052.jpg "/>
Special usage of the while loop:
Traverse each line of the file:
while read VARIABLE; Do
Loop body
Done </path/from/some_file
Example: Find all users whose UID is even, display their user name and ID number;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/52/wKiom1X6MybzY_ojAAHCpypEc8Y110.jpg "title=" For5.jpg "alt=" Wkiom1x6mybzy_ojaahcpypec8y110.jpg "/>
3,UNITL Cycle
UNITL Loop Statement Format:
Until CONDITION; Do
Loop body
Modified expressions for cyclic control variables
Done
Entry condition: When condition is "false"
Exit Condition: When condition is "true"
Example: Sum of all positive integers within 100
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/50/wKioL1X6NpjiCEwTAADatM875AQ147.jpg "title=" Until.jpg "alt=" Wkiol1x6npjicewtaadatm875aq147.jpg "/>
4, loop control (break,continue)
Circular control Meaning:
The # Break command does not perform the current loop within the body break, and the following statement exits from the current loop.
# Continue command is a program that ignores the following statement in this body, starting from the loop header
Break [n]: End loop prematurely; [n] indicates a few layers of loop to jump (end)
Continue [n]: End the cycle in advance and go directly to the next round; [n] continue to cycle into the first round
Use of loop control:
While loop:
While CONDITION; Do
.......
if CONDITION2; Then
Break [n]
Fi
Done
While CONDITION; Do
......
if CONDITION2; Then
Continue [n]
Fi
......
Done
Example: The sum of all even numbers within 100; continue use
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/50/wKioL1X6PPSiYnLmAAG8uHQUVVg581.jpg "title=" w1.jpg "alt=" Wkiol1x6ppsiynlmaag8uhquvvg581.jpg "/>
Dead Loop:
While true; Do
Loop body
if condtion; Then
Break
Fi
Done
until false; Do
Loop body
if CONDITION; Then
Break
Fi
Done
Example: every 3 seconds to see if the current system is known as "Gentoo" user login;
If a visit to Gentoo is logged in, it shows that Gentoo is logged in;
If not logged in, the display remains in the future and shows how many times it is already viewed;
Note Break uses
While using break
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/50/wKioL1X6P6DhUgqDAAIKmpOc5NI673.jpg "title=" w2.jpg "alt=" Wkiol1x6p6dhugqdaaikmpoc5ni673.jpg "/>
Until using break
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/53/wKiom1X6PmLBfANhAAIapd7SDW4685.jpg "title=" uu.jpg "alt=" Wkiom1x6pmlbfanhaaiapd7sdw4685.jpg "/>
5, use of functions
function use:
Function: Functions
Encapsulate a piece of code with a separate function and give it a name, and then use the given function name directly to
Call the whole code;
Function: Code reuse, modular programming;
How to use the function:
Define first: Write function code
Call after: give the function name and pass the parameters on demand
Define the method:
(1) Function F_name {
function body
}
(2) F_name () {
function body
}
Call Function:
F_name [Argu1, ARGU2, ...]
Custom function state return value:
return [#]
0: Success
1-255: Failure
Note: When the function code executes, once the return is encountered, the function code terminates and the function returns;
Example: Execute start,stop,restart to implement the file state judgment separately:
The Start,stop,restart is used as the function body, and the case uses the module call function.
#!/bin/bash#prog=$ (basename $0) lockfile= "/var/lock/subsys/$prog" #echo $lockfileif [ $# -lt 1 ]; then echo "usage: $prog start|stop|restart| Status " exit 1fistart () { if [ -f $lockfile ]; thenecho "$prog is started yet." elsetouch $lockfile && echo "starting $prog ok ..." | | echo "starting $prog failed ..." fi}stop () { if [ -f $lockfile ]; thenrm -f $lockfile && echo "Stop $prog ok .... | | echo "stop $prog failed ..." elseecho "$prog is stopped yet. " fi}restart () { if [ -f $lockfile &NBSP;];&NBSP;THENRM -f $lockfile && touch $lockfile && echo "restarting $porg ok ..." elsetouch $lockfile && echo $prog is stopped, Starting $prog ok. " fi}status () { if [ -f $lockfile ]; thenecho "Running ..." elseecho "Stopped ..." fi}case $ 1 instart) start ;; Stop) stop ;; Restart) restart ;; Status) status ;; *) echo "usage: $prog start|stop|restart|sttus" exit 1esac[[email protected] /]# ./test.sh startstarting test.sh ok ... [[email protected] /]# cd /var/lock/subsys/[[email protected] subsys]# Lsnetwork test.sh #start创建了test. sh file [[email protected] subsys]# cd -[[email protected] /]# ./test.sh stop[[email protected] /]# cd -[[email protected] subsys]# lsnetwork #stop删除了test. sh file [[email protected] subsys]# cd -[[email protected] /]# ./test.sh restart[[email protected] /]# cd -[[email protected] subsys]# lsnetwork test.sh #restart执行了先删除再创建test. sh file
Function Modular Invocation
Modular programming
Function: Separate the code in the script file into multiple segments and put it in a different file
Suppose the/root/bin/srv directory has two files:
(1) Function file
(2) Script file
Using configuration files for scripts
Only variables are defined in a file
Script file Source This variable defines the file
Scope of the variable:
Local variables:
Local Variable=value
Survival time:
function execution begins, until function returns to the end;
Example: test01.sh function definition variable, test02.sh script reference test01.sh
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/53/wKiom1X6RqeBRLYXAAF5rwFosAQ220.jpg "title=" Hanshu.jpg "alt=" Wkiom1x6rqebrlyxaaf5rwfosaq220.jpg "/>
Summary of L7.3 Loop statements