Shell's While loop

Source: Internet
Author: User

The format of the while loop:

 while expression  Do Commandcommand 、、、  Done

1. Counter-controlled while loop:
Used primarily for the number of data and strings that have been accurately known to be entered.

Example:

#!/bin/bashint=1 while (($Int. <=5  )) do echo $int"int++" Done

2. End tag Control while loop
Mainly used to not know the number of read data, but you can set a special data value to end the loop, the special value is called the end tag, by prompting the user input to operate.

Example:

#!/bin/bash# using the end tag to control the while loop implementation guess 1~number within 10Echo "Please input the num (1~~10):"Read num while[[$num! =4 ]] Doif[$num-lt4]; Then  Echo "Too small,try again."Read numelif[$num-GT4]; Then  Echo "Too big,try again."Read numElseExit0fi DoneEcho "yes,you is right!"   

3. Flag-controlled while loop
User input flag value to control the end of the loop

Example:

#!/bin/BashEcho "Please input the num:"Read numsum=0I=1Signal=0# while[[$signal! =1 ]] while($signal! =1)) Doif[$i-eq $num]; Then Let"signal=1" Let"sum+=i"  Echo "1+2 、、、 + $num = $sum"Else Let"Sum=sum+i" Let"i++"fi Done

4. Command-line controlled while loop
Example:

#!/bin/bashecho"echo "  while""  ]"doesecho $1shift done

Shell's While loop

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.