For and while loops in the shell detailed summary _linux shell

Source: Internet
Author: User

One, for loop
1. Digital Segment Form

Copy Code code as follows:

For i in {1..10}
Todo
Echo $i
Done

2. Detailed list (character and number of items are not many)
Copy Code code 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)
Copy Code code 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
Copy Code code 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
Copy Code code 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

Copy Code code 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))
Copy Code code 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
Copy Code code 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
Copy Code code 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.