Shell Script Programming Loop statements

Source: Internet
Author: User

Repeating a series of commands while running a script is common, and we need to use loop statements to do this.

One, for command

Format: for variable in list;

Loop body

Done

The for command iterates through each value in the list and exits the loop after the traversal is complete.

There are several forms of the list:

1, a series of values defined in the command;

2, variables;

3, order;

4, directory;

5, documents.

Example 1:

650) this.width=650; "title=" 1451812909445180.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451812909445180.jpg "/>

650) this.width=650; "style=" text-align:center; "title=" 1451834452379687.jpg "alt=" 1.jpg "src=" http:// Www.178linux.com/ueditor/php/upload/image/20160103/1451834452379687.jpg "/>

In the example above, use the {initial value: End value} format to represent the numeric value of an interval.

Example 2: Use a variable as a list:

650) this.width=650; "title=" 1451813025841146.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451813025841146.jpg "/>

Example 3: Generate a list with a command:

650) this.width=650; "title=" 1451834846978770.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451834846978770.jpg "/>

In the previous example, using the SEQ command to generate the list, the SEQ command is in the format: SEQ [initial value step] terminating value

650) this.width=650; "title=" 1451813301230401.jpg "alt=" 3.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451813301230401.jpg "/>

The above two examples use anti-quotes to get the result of the command run.

Example 4: Use the directory as a list:

650) this.width=650; "title=" 1451834311885880.jpg "alt=" 4.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451834311885880.jpg "/>

Second, C language style for command

Format: for (variable assignment; loop termination condition; step)); Do

Loop body

Done

Example: for 1 plus to 100 and:

650) this.width=650; "title=" 1451835193946183.jpg "alt=" 5.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451835193946183.jpg "/>

Iii. until order

Format: Until condition; Do

Statement

...

Done

When a condition in the until command is satisfied, the loop exits.

Instance:

650) this.width=650; "title=" 1451835795903908.jpg "alt=" 6.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451835795903908.jpg "/>

Four. While command

Format: While condition; Do

Statement

...

Done

The while command enters the loop when the condition is met.

Instance:

650) this.width=650; "title=" 1451835996609630.jpg "alt=" 7.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160103/1451835996609630.jpg "/>

V. Control cycle

1. Break command

function: Exits a loop that is in progress.

Instance: Exits the loop if the variable i is 3.

650) this.width=650; "title=" 1451837151654175.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160104/1451837151654175.jpg "/>

2. Continue command

Function: Advance into the next round of circulation.

Example: Add a number from 1 to 20 and 51 to 100:

650) this.width=650; "title=" 1451837327444231.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160104/1451837327444231.jpg "/>

Vi. the use of the while

1. Dead Cycle

Format: while:; Do

Statement

Done

Instance:

650) this.width=650; "title=" 1451838566432871.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160104/1451838566432871.jpg "/>

2. Read the contents of the file into the loop by line

Format: while read variable name; Do

Loop body

Done < files

Example: Read the contents of the/etc/passwd file line by row:

650) this.width=650; "title=" 1451839645380871.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160104/1451839645380871.jpg "/>



Shell Script Programming Loop statements

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.