Loop syntax in the shell
Yun Zhengjie
Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.
A. For Loop1. Syntax format 1
1 for inch Value 1 value 2 value 3 ... 2 Do 3 Source Code 4 Done
2. Syntax Format 2
1 for (initial value; cyclic control condition; variable change)) 2 Do 3 Source Code 4 Done
3. Case show One
1[Email protected] backup]# MoreFor1.SH 2#!/bin/Bash3 # @author: Yinzhengjie4#blog: http://Www.cnblogs.com/yinzhengjie5 #EMAIL: [EMAIL protected]6 7 forIinch 1 2 38 Do9 Echo "The first $i cycle!"Ten Done One[Email protected] backup]#
4. Case show two (summation)
1[Email protected] backup]# MoreFor3.SH 2#!/bin/Bash3 # @author: Yinzhengjie4#blog: http://Www.cnblogs.com/yinzhengjie5 #EMAIL: [EMAIL protected]6 7 sum=08Read-t --P"Please enter the start number>>>"Startnumber9Read-t --P"Please enter an end number>>>"EndnumberTen One for(i= $StartNumber; i<= $EndNumber; i=i+1)) A Do - sum=$(($sum+$i)) - Done the - Echo "the sum from $startnumber to $endnumber is: $sum" -[Email protected] backup]#
5. Case show three (bulk add users)
1[Email protected] backup]# MoreUseradd.SH 2#!/bin/Bash3 # @author: Yinzhengjie4#blog: http://Www.cnblogs.com/yinzhengjie5 #EMAIL: [EMAIL protected]6 7 8Read-p"Please input username (default"Yinzhengjie"):"-T -username9Read-p"Please input the number of users:"-T -UsersnumberTenRead-p"Please input the password of users:"-T -Password One A if[ -Z $username] - Then -Username=Yinzhengjie the fi - - - if[!-Z"$username"-A! -Z"$UsersNumber"-A! -Z"$password" ] + Then -y=$ (Echo$UsersNumber |sed 's/[0-9]//g') + if[-Z"$y" ] A Then at for((i=1; i<= $UsersNumber; i=i+1 )) - Do -Useradd"$username $i"&>/dev/NULL - Echo$password |passwd--stdin"$username $i"&>/dev/NULL - Done - fi in fi - to[Email protected] backup]#
two. While Loopthe while loop is an indefinite loop, also known as a conditional loop. As long as the conditional judgment is established, the cycle will continue. As long as the conditional judgment is not established, the cycle will stop. This is not the same as for the fixed loop. 1. Syntax format
1 while [Conditional judging type] 2 Do 3 Source code 4 done
2. Case Show
1[Email protected] shell]# More while.SH 2#!/bin/Bash3 # @author: Yinzhengjie4#blog: http://Www.cnblogs.com/yinzhengjie5 #EMAIL: [EMAIL protected]6 7 sum=08Read-t --P"Please enter the start number>>>"Startnumber9Read-t --P"Please enter an end number>>>"EndnumberTen One while[$StartNumber-le $EndNumber] A Do - sum=$(( $sum+$StartNumber)) -startnumber=$ (($StartNumber +1 )) the Done - -startnumber=$ ($StartNumber-$EndNumber)) - + Echo "the sum from $startnumber to $endnumber is: $sum" -[Email protected] shell]#
three. Until CycleThe until loop, in contrast to the while loop, loops as long as the conditional judgment is not true, and executes the loop program. Once the loop condition is established, the loop is terminated. 1. Syntax format
1 until [conditional expression] 2 Do 3 Source code 4 done
2. Case Show
1[Email protected] shell]# More until.SH 2#!/bin/Bash3 # @author: Yinzhengjie4#blog: http://Www.cnblogs.com/yinzhengjie5 #EMAIL: [EMAIL protected]6 7 sum=08Read-t --P"Please enter the start number>>>"Startnumber9Read-t --P"Please enter an end number>>>"EndnumberTen One until[$StartNumber-GT $EndNumber] A Do - sum=$(( $sum+$StartNumber)) -startnumber=$ (($StartNumber +1 )) the Done - -startnumber=$ ($StartNumber-$EndNumber)) - + Echo "the sum from $startnumber to $endnumber is: $sum" -[Email protected] shell]#
Loop syntax in the shell