Loop syntax in the shell

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.