Example of using the while loop in Shell

Source: Internet
Author: User
  1. #! /Bin/sh
  2. # Filename: 2.sh
  3. B = 9
  4. E = 15
  5. Tmpb = $ B
  6. While [$ tmpb-Le $ E]
  7. Do
  8. Echo $ tmpb
  9. # Tmpb = 'expr $ tmpb + 1' // OK
  10. Tmpb = $ (expr $ tmpb + 1)
  11. Done

After the script is executed, the output result is:

9
10
11
12
13
14
15

 

Note: The functions of row 9th are the same as those of row 10th.

 

 

The following example uses a loop. You must enter a number to exit.

  1. #! /Bin/sh
  2. # Filename: B
  3. While:
  4. Do
  5. Echo-n "input a number :"
  6. Read Val
  7. Expr $ Val + 1>/dev/null 2>
  8. Res = $?
  9. If ["$ Val" = ""] | ["$ res "! = "0"]; then
  10. Echo "what you input is not a number! "
  11. Else
  12. Echo "what you input is: $ Val"
  13. Break;
  14. Fi
  15. Done

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.