Article title: four tips on shell program design. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
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, 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.
The following is an example.
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, but one or more files in one of the directories have just been modified. now I forget the files I just modified, so I am relying on the reseller to raise a fund for Ke yunnai, the reef in the Xianxian region? Elliptical? Lazy? 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.
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