The for and while loops in the shell are summarized in detail

Source: Internet
Author: User
Tags min

This article mainly introduced in the shell for And while loop detailed summary, this article explained for the For Loop's digital section form, detailed lists, to the file circulation, while loop's three kinds of use situation and so on content, needs the friend to be possible to refer to under

One, for loop

1. Digital Segment Form

The code is as follows:

For i in {1..10}

Todo

Echo $i

Done

2. Detailed list (character and number of items are not many)

The code is as follows:

For File in 1 2 3 4 5

Todo

Echo $File

Done

3. Circulation of existing documents

Copy code code as follows:

For shname in ' LS *.sh '

Todo

Name= ' echo ' $shname | Awk-f. ' {print '} '

Echo $name

Done

4. Lookup loop (LS data too large can also use this method)

The code is as follows:

For Shname in ' find. -type f-name "*.sh"

Todo

Name= ' echo ' $shname | awk-f/' {print $} '

Echo $name

Done

5. (Grammar loop--a bit like C syntax, but remember double bracket

The code is as follows:

For ((i=1;i<100;i++))

Todo

if ((i%3==0))

Then

Echo $i

Continue

Fi

Done

6.seq form starting from 1

The code is as follows:

For i in ' SEQ 100 '

Todo

if ((i%3==0))

Then

Echo $i

Continue

Fi

Done

Two, while loops

1.while Circular note for square brackets [], and note that spaces

The code is as follows:

Min=1

max=100

While [$min-le $max]

Todo

Echo $min

min= ' expr $min + 1 '

Done

2. Double bracket form, internal structure somewhat like C syntax, note assignment: i=$ (($i + 1))

The code is as follows:

I=1

while (($i <100))

Todo

if (($i%4==0))

Then

Echo $i

Fi

i=$ (($i + 1))

Done

3. Read from the configuration file and can control the number of processes

The code is as follows:

Max_run_num=8

Cat cfg/res_card_partition.cfg |grep-v ' ^$ ' |grep-v ' # ' | grep-v grep |while Read partition

Todo

Nohup sh inv_res_card_process.sh $partition >log/rescard$partition.log 2>&1 &

While [1-eq 1]

Todo

Psnum= ' Ps-ef | grep "Inv_res_card_process" | Grep-v "grep" | Wc-l '

If [$psNum-ge $MAX _run_num]

Then

Sleep 5

Else

Break

Fi

Done

Done

Three. Loop control statement

The code is as follows:

# Break command does not execute the current loop body break the following statement exits from the current loop.

# Continue command is the procedure to ignore the following statements in this body, starting from the loop header

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.