Shell Scripting Learning 19 shell while loop

Source: Internet
Author: User

The while loop is used to continuously execute a series of commands and to read data from the input file; the command is usually a test condition. The format is:

While Commanddo Statement (s) to being executed if command is Truedone

The command finishes, control returns to the top of the loop, and starts from the beginning until the test condition is false.

The following is a basic while loop, and the test condition is: Returns True if counter is less than 5. Counter starting from 0, counter plus 1 for each cycle of processing. Run the above script, return the number 1 to 5, and then terminate.

    1. COUNTER=0
    2. While [ $COUNTER -lt 5 ]
    3. Do
    4. COUNTER=' expr $COUNTER +1 '
    5. echo $COUNTER
    6. Done

Run script, Output:

12345


The while loop can be used to read keyboard information. In the following example, the input information is set to the variable film, ending the loop by <Ctrl-D>.

Copy Plain Text New window
    1. echo ' type <CTRL-D> to terminate '
    2. echo- n ' Enter your most liked film: '
    3. While read FILM
    4. Do
    5. echo "yeah! Great film The $FILM "
    6. Done

Run the script with the output similar to the following:

Type <CTRL-D> to terminateenter your most liked film:sound of musicyeah! Great film The Sound of Music

Shell Scripting Learning 19 shell 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.