Shell Getting Started Tutorial: Process Control (6) while loop

Source: Internet
Author: User

Syntax for While loop:

While condition test
Do
Command Area
Done

Example:

#!/bin/bashdeclare-i i=1declare-i Sum=0while ((i<=10)) Dolet Sum+=ilet ++idoneecho $sum

Use while to read file contents

While loop, the classic usage is to go with the switch input, read the contents of the file, as follows:

#!/bin/bashwhile Read Linedoecho $linedone < Cvsfile
    • Line 3, use read to read the data from the standard input, put into the variable lines, if the read data is not empty, enter the loop.
    • Line 6, the contents of the Cvsfile to the input, to read reading.

One more example:

#!/bin/bashifs= ': ' While read F1 F2 F3 f4 f5 f6 f7doecho ' account: $f 1,login Shell Yes: $f 7 "Done <"/etc/passwd "

Line 3, read/etc/passwd each line account data, and put the fields into the variable F1~F7.

While Infinite loop

Whlie ((1)) Doecho ' Sleepless ' done

It is also possible to change ((1)) to [1].

Or:

Whlie truedoecho ' Night sleepless ' done

Or:

Whlie:d oecho ' Night sleepless ' done

Type CTRL + C to terminate the loop.

Shell Getting Started Tutorial: Process Control (6) 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.