Shell's for, while loop

Source: Internet
Author: User

One, for loop


[email protected] shell]# cat for.sh

#!/bin/bash

For i in ' seq 1 10 '; Do

echo "$i"

Done


With this script you can see the basic structure of the For loop:

The condition of the for variable name in the loop; do Commanddone

[Email protected] shell]# sh for.sh

1

2

3

4

5

6

7

8

9

10


Example 2:


[email protected] shell]# cat for2.sh

#!/bin/bash

For a in ' LS '; Do

echo "$a"

Done



[Email protected] shell]# sh for2.sh

case1.sh

case.sh

for2.sh

for3.sh

for.sh

if1.sh

if.sh


Example 3

[email protected] shell]# cat for3.sh

#!/bin/bash

For file in ' Vmstat '; Do

echo "$file"

Done

For i in ' Cd/shell && ls '; Do

echo "$i"

Done


Reference system commands need to be inverted, others do not

[[email protected] shell]# for I in 1 4 5 3 A; Do echo "$i"; Done

1

4

5

3

A

A




Second, while loop


[email protected] shell]# cat while.sh

#!/bin/bash

A=6

While [$a-ge 1]; Do

Echo $a

a=$[$a-1]

Done


The while loop format is also simple:

While condition; Do Commanddone

[Email protected] shell]# sh while.sh

6

5

4

3

2

1



Example 2

[email protected] shell]# cat while2.sh

#!/bin/bash

While:; Do

Seq 1 3

Done


Replace the loop condition with a colon, so that you can do a dead loop

[Email protected] shell]# sh while2.sh

1

2

3

1

2

3

1

2


This article from "Unchanged Time---hu" blog, reprint please contact the author!

Shell's for, 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.