Shell programming for while until loops

Source: Internet
Author: User

The shell's for loop format is as follows:

For variable in list

Do

...

Done

A list is a series of values that each value is separated by a space, once per loop, and the next value in the list is assigned to the variable

The in list is optional, if you do not use it, the For loop uses the command-line positional parameters

Example

Numbers in the output list

For x in 1 2 3 4 5

Do

Echo $x

Done

Strings in the output list

For x in "This Is My Girl"

Do

Echo $x

Done

All files in the output directory

For FI in *

Do

Echo $fi

Done

The while loop is used to continuously execute a series of commands and to read data from the input file.

While CMD

Do

...

Done

Example

Loop output 0-5 Digits

count=0;

While [$count-lt 5]

Do

Echo $count

Count= ' expr $count + 1 '

Done

While the while can also be used to enter an indefinite number of keyboard information ctrl+d can end the loop

While read film

Do

echo "Your most favrite film is $film"

Done

The Until loop executes a series of commands until the condition is true. The Until loop and the while loop are just the opposite of the processing mode. The general while loop is better than the until loop, but at some point it is only rare that the until loop is more useful.

Example:

#!/bin/bash

A=0

Until [! $a-lt 10]
Do
Echo $a
A= ' expr $a + 1 '
Done

Jump out of the loop

In the loop, sometimes you need to force out of the loop when the loop end condition is not reached, like most programming languages, the shell also uses break and continue to jump out of the loop.

Break command

The break command allows you to jump out of all loops (all loops after the execution is terminated).

Count=0

While [$count-lt 10]

Do

If [$count-eq 5]

Then

echo "Find You ${count}"

Break

Fi

Count= ' expr $count + 1 '

Done

Continue command

The continue command is similar to the break command, with only a little difference, and it does not jump out of all loops and just jumps out of the current loop.

#!/bin/bash

Count=1

While [$count-lt 5]

Do

Count= ' expr $count + 1 '

If [$count-eq 3]

Then

echo "Find You $count"

Continue

Fi

Echo $count

Done

Shell programming for while until loops

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.