While loop "Go" in the shell

Source: Internet
Author: User

Turn from: Http://blog.chinaunix.net/uid-25880122-id-2901409.htmlwhile loop format
    1. While expression
    2. Do
    3. Command
    4. Command
    5. ```
    6. Done
1. The counter-controlled while loop is used primarily for the number of data and strings that have been accurately known to be entered. Example
    1. 1 #!/bin/sh
    2. 2 Int=1
    3. 3 while (($int <=5))
    4. 4 Do
    5. 5 echo $int
    6. 6 Let "int++"
    7. 7 Done
2, end tag control while loop is 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
  1. 1 #用脚本演示使用结束标记控制while循环实现猜1 the number in the ~
  2. 2 #!/bin/sh
  3. 3
  4. 4 echo "Please input the num (1~~10):"
  5. 5 Read num
  6. 6 while [[$num! = 4]]
  7. 7 do
  8. 8 If [$num-lt 4]
  9. 9 Then
  10. echo "Too small, Try again ..."
  11. Read num
  12. elif [$num-GT 4]
  13. Then
  14. echo "Too big, Try again ..."
  15. Read num
  16. + Else
  17. Exit 0
  18. Fi
  19. Done
  20. echo "Yes, you is right!"
3, Peugeot control while loop user input flag value to control the Loop end example
  1. 1 #!/bin/sh
  2. 2 echo "Please input the num:"
  3. 3 Read num
  4. 4 sum=0
  5. 5 I=1
  6. 6 signal=0
  7. 7 while [[$signal! = 1]]
  8. 8 Do
  9. 9 If [$i-eq $num]
  10. Ten Then
  11. One-let "signal=1"
  12. "Sum+=i"
  13. echo "1+2 、、、 + $num = $sum"
  14. + Else
  15. "Sum=sum+i"
  16. "I++"
  17. + fi
  18. Done
4. Example of a while loop with command-line control
    1. 1 #!/bin/sh
    2. 2
    3. 3 echo "Please input arguements is $#"
    4. 4 echo "What do you input:"
    5. 5 while [[$*! =] "]
    6. 6 do
    7. 7 echo $
    8. 8 shift
    9. 9 Done

While loop "Go" in the shell

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.