Shell common knowledge (4)

Source: Internet
Author: User

1. while Loop

While Command Format
While condition table
Do
Command table
Done
Execution Process
Shell first executes the condition table. If the exit status of the last statement in the condition table is zero, execute the command table in the environment. After the execution, check the condition table, if the exit status is zero, the execution continues until the exit status of the last statement in the condition table is non-zero. if the exit status is zero, the condition is True.
For example, if the content of the shell file is as follows:
Sum = 0
I = 0
While true # true is the system keyword indicating true
Do
I = 'expr $ I + 1'
Sum = 'expr $ Sum + $ I'
If [$ I = "100"]
Then
Break;
Fi
Done
Echo $ I $ Sum
The program shows 100 5050
The calculation of this program is to add 1 to 100
Next, let's change this program.
Sum = 0
I = 0
While [$ I! = "100"]
Do
I = 'expr $ I + 1'
Sum = 'expr $ Sum + $ I'
Done
Echo $ I $ Sum
The modified program operation result is the same as above, but the program is more concise than above.
In this loop, you can also use until as the test condition. It is exactly the opposite of the while test condition, that is, when the condition is false, the statement in the loop will continue to be executed, otherwise, exit the loop body. This example is also used below.
Sum = 0
I = 0
Until [$ I = "100"]
Do
I = 'expr $ I + 1'
Sum = 'expr $ Sum + $ I'
Done
Echo $ I $ Sum
When I is not equal to 100, the loop is when the condition is false. Otherwise, the loop exits. The first example is when I is not equal to 100.
Cycle, that is, when the test condition is true.
II. for Loop

Command Format:
For variable in Name List
Do
Command list
Done
The name list here is a list of strings separated by spaces. shell extracts a string from the name table each time it executes the for loop and assigns it to the loop variable as the variable value.

When writing a for statement, you can also omit the in name list section, which means that the current location parameter is used to replace the name list.

For example, there are two directories in your computer, one is aa, and the other is bb. There are five identical files in these two directories, however, one or more files in one of the directories have just been modified. Now I forget the files I just modified. Then I am searching by known code.

The procedure is as follows:
For File in a1 a2 a3 a4 a5
Do
Diff aa/$ File bb/$ File
Done
The following is an example without a name list.
For File
Do
Echo $ Filw
Done
The file content is saved in a. sh and can be executed
When we execute this shell program, the command line is as follows:
A. sh a1 a2 a3 a4 a5
The execution result is as follows:
A1
A2
A3
A4
A5
In this example, we can see that the command line parameters are read one by one.
Iii. Loop Control statements

The break command does not execute the statements in the current cycle following the break.

The continue command is used by the program to ignore the following statements in the current cycle and start execution from the loop header.


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.