The while command in Linux

Source: Internet
Author: User

The while command is a bit like a combination of if-then and for loops. The while test command returns a 0 exit state, looping through a set of commands.

While basic format:

While Test command

Do

Other commands

Done

Example:

#!/bin/bash

Varl=5

While [$varl-GT 0]

Do

Echo $varl

varl=$[$varl-1]

Done

[Email protected] ~]#./test10.sh

5

4

3

2

1

Note: Because while is detected when the exit status is 0, the loop is stopped only if the output is not 0.

varl=$[$varl-1] The result is not 0


Using multiple test commands

#!/bin/bash

Var1=3

While Echo $var 1

[$var 1-ge 0]

Do

echo "The Is inside the loop"

var1=$[$var 1-1]

Done

[Email protected] ~]#./test11.sh

3

The is inside the loop

2

The is inside the loop

1

The is inside the loop

0

The is inside the loop

-1

In multiple-line commands, all test commands are executed in each of the failed generations, including the last one to fail the test command



The while command in Linux

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.