Shell Getting Started Tutorial: Process Control (5) for loop

Source: Internet
Author: User

The For loop works by taking the element one by one of the serial element out, putting it into the set variable sequentially, and then repeating the enclosed command area (between do and done) until all the elements have been exhausted.

Where serial is a combination of strings, separated from each other by $IFS defined delimiters, such as whitespace, which are called fields.

The syntax structure for the for IS as follows:

For variable in serial
Do
Command Area
Done

Example:

For I in 1 2 3 4doecho $idone

For the syntax of the For loop, the "in serial" paragraph can be omitted without writing, at this point, bash will be read by the command line parameters, rotation into the variable, the role of the following usage:

For variable in [email protected]do command area done

[email protected] represents all the parameters of the command line.

The For loop is often used to split string rows, removing field element values.

Ifs= ': ' pl= ' Root:x:0:0:root:/root:/bin/bash ' for f in $PLdoecho $fdone

It is a classic practice to take out fields from each row of the CSV file. A CSV file is a plain text file that separates data fields (commas) and is suitable for serving as intermediaries for data import and export between databases.

Here's how to remove each row of a CSV file:

#!/bin/bashdeclare-i I=0for line in $ (cat cvsfile.txt) doi=i+1echo-n "The field of the first $i is:" save_ifs= $IFSIFS = ', ' for f in $linedoech O-n $f ' doneifs= $save _ifsecho done

The For loop is also frequently used for files in the processing directory:

#!/bin/bashdir= "/root" for f in $ (ls $DIR) doecho ' file: ' $fdone

Lists files under the /root directory, but does not include hidden files.

Let's look at an example:

#!/bin/bashdir= "/var" CD $DIRfor F in $ (LS $DIR) do[-D $f] && du-s $fdone

This program is used to list the amount of disk space occupied by each subdirectory under the/var directory.

Another way to use for:

for (initial conditions; termination criteria; abnormal items))
Do
Command Area
Done

Example:

#!/bin/bashdeclare-i I sumfor ((i=1; i<=10; i=i+1)) Dolet Sum+=idoneecho $sum

For Infinite Loop:

for ((; 1;)) do command area done

To break the infinite loop, you have to type Ctrl + C.

Shell Getting Started Tutorial: Process Control (5) for 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.