Loop statements in the shell

Source: Internet
Author: User
Tags glob

For syntax format

for Var in list;do

Commands

Done

Where list can contain:

1) write directly

 for  in a b c D;  do    Echo $alpha  Done

2) variables

list="a b c d" for in $list;  do    Echo $alpha  Done

The variable is replaced when the shell executes, and after the list variable is replaced, the For loop is identical to the form in 1. However, if you add quotation marks to the $list, that is, if you write the following form:

list="a b c d"for in"$list";  do    Echo $list  Done

After the shell variable is replaced by:

list="a b c d"for in"a b c d";  do    Echo $list  Done

At this point the output is only one line a B c d.

3) Shell command

 for  in 'cat alpha.txt ';  do    Echo $alpha  Done

Assuming that the content in the Alpha.txt file is a B c D, then the contents of the file are read and iterated by using ' Mr. Foo, and the result is the same as 1

4) Read the file directory

 for file  in $HOME/A/*;d o    echo $filedone

The above code advanced Line wildcard Glob extension, assuming that directory A has 2 files 1.txt, 2.txt, a folder B, then after the extension is actually:

 for file  in $HOME/a/1. txt $HOME/a/2. txt $HOME/a/b;  do    echo $file done

At this point, the value of file is $home/a/1.txt, $HOME/a/2.txt, $HOME/a/b.

However, assuming that a is not a directory and is a file, the wildcard extension fails at this time, and the value of file is directly $home/a/*.

One of the things to note here is that the shell Glob is being extended, so the extension cannot cross the file boundaries, and in other words, if there are files under the B directory, the files cannot be extended, that is, the extension cannot go beyond folder B

Ifs

In the For loop, how the list is split is determined by IFS, by default the value of IFS is:

Tab

Space

Line break

You can assign a value to the IFS again:

ifs=: #此时分隔符为: IFS=:; #此时分隔符为: and;

C-style for loop

for ((i = 0; i < i++));d o

Commands

Done

The style here is the same as in C, where the variable I can be any variable

While loop

While Command;do

Commands

Done

The command can be either shell command or test condition. If the command returns a value of 0 or if the test is true, it is executed, otherwise it is not executed.

It is important to note that while you can use multiple conditions, only the last condition works:

var=00 ];  do    Echo $var  Done

In this code, although the first condition is not valid at first, it is the last condition that works, so this is a wireless loop

Until cycle

Until Command;do

Commnds

Done

As with the while, the only difference is that if the command returns 0, it is not executed, otherwise it executes

Loop statements in the shell

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.