Loop statement instance in Shell

Source: Internet
Author: User

1. For Loop statements
Basic for loop of instance 1.1


#! /Bin/bash

For X in one two three four
Do
Echo number $ x
Done
 


Note: A "for" loop always receives a certain type of word list after the "in" statement. In this example, four English words are specified, but the word list can also reference files on the disk, or even file wildcards.
Instance 1.2


#! /Bin/bash
For X in/var/log /*
Do
# Echo "$ X is a file living in/var/log"
Echo $ (basename $ X) is a file living in/var/log
Done
 


Note: $ X obtains the absolute path file name. You can use the "basename" executable program to remove the preceding path information. If you only reference files in the current working directory (for example, if you enter "for X in *"), the generated file list will have no prefix for path information.
Instance 1.3 performs a for loop on location parameters


#! /Bin/bash
For thing in "[email protected]"
Do
Echo you typed $ {thing }.
Done


Instance 1.4 for loop generation cycles using seq


#! /Bin/bash
For J in $ (SEQ 1 5)
Do
Echo $ J
Done


Note: For a fixed number of cycles, you can use the seq command to implement them without variable auto-increment.

2. While loop statement
Instance 2.1 cyclically outputs numbers 1 to 10


#! /Bin/bash
Myvar = 1
While [$ myvar-le 10]
Do
Echo $ myvar
Myvar = $ ($ myvar + 1 ))
Done
 


Note: As long as the specified condition is true, the "while" statement will be executed.

3. Until loop statement
Instance 3.1 cyclically outputs numbers 1 to 10
The "until" statement provides the opposite functionality of the "while" Statement: as long as the specific conditions are false, they are repeated. The following is an "until" loop with the same function as the previous "while" loop.


#! /Bin/bash
Myvar = 1
Until [$ myvar-GT 10]
Do
Echo $ myvar
Myvar = $ ($ myvar + 1 ))
Done
 

 

It is also very important that the auto-increment of temporary variables will inevitably be used in the loop. for how to implement the auto-increment of variables in shell, see one of my previous

Variable auto-increment is often used when writing loops in Linux Shell. Now, let's summarize the auto-increment method of integer variables.
As I know, bash currently has five methods:
1. I = 'expr $ I + 1 ';
2. Let I + = 1;
3. (I ++ ));
4. I = $ [$ I + 1];
5. I = $ ($ I + 1 ))
A simple example is as follows:
#! /Bin/bash
I = 0;
While [$ I-lt 4];
Do
Echo $ I;
I = 'expr $ I + 1 ';
# Let I + = 1;
# (I ++ ));
# I = $ [$ I + 1];
# I = $ ($ I + 1 ))
Done

In addition, for a fixed number of cycles, the seq command can be used to achieve the implementation, without the need for variable auto-increment; the example is as follows:
#! /Bin/bash
For J in $ (SEQ 1 5)
Do
Echo $ J
Done

Http://www.linuxidc.com/Linux/2011-05/35893.htm

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.