Linuxshellawk process control statement (if, for, while, do)

Source: Internet
Author: User
In awk, flow control statements, syntax structure, and C language type. The usage of each statement is as follows:

In the while, do-while, and for statements of linux awk, break and continue statements can be used to control the process and exit using exit statements. The break interrupts the current loop and jumps out of the loop to execute the next statement. If is the process selection usage. In awk, flow control statements, syntax structure, and C language type. The following describes the usage of each statement.

I. condition judgment statement (if)


Copy codeThe code is as follows:
If (expression) # if (Variable in Array)
Statement 1
Else
Statement 2

In the format, "statement 1" can be multiple statements. if you want to facilitate the Unix awk judgment and facilitate your own reading, you 'd better enclose multiple statements. The Unix awk branch structure can be nested in the following format:


Copy codeThe code is as follows:
If (expression)

{Statement 1}

Else if (expression)
{Statement 2}
Else
{Statement 3}


Copy codeThe code is as follows:
[Chengmo @ localhost nginx] # awk 'In in {
Tested = 100;
If (test> 90)
{
Print "very good ";
}
Else if (test> 60)
{
Print "good ";
}
Else
{
Print "no pass ";
}
}'
Very good

 
Each command statement can end.

II. loop statements (while, for, do)

1. while statement

Format:

While (expression)

{Statement}

Example:


Copy codeThe code is as follows:
[Chengmo @ localhost nginx] # awk 'In in {
Tested = 100;
Total = 0;
While (I <= test)
{
Total + = I;
I ++;
}
Print total;
}'
5050

2. for loop

The for loop has two formats:

Format 1:

For (variable in array)

{Statement}

Example:


Copy codeThe code is as follows:
[Chengmo @ localhost nginx] # awk 'In in {
For (k in ENVIRON)
{
Print k "=" ENVIRON [k];
}
}'

AWKPATH =.:/usr/share/awk
OLDPWD =/home/web97
SSH_ASKPASS =/usr/libexec/openssh/gnome-ssh-askpass
SELINUX_LEVEL_REQUESTED =
SELINUX_ROLE_REQUESTED =
LANG = zh_CN.GB2312
......

Note: ENVIRON is an awk constant and a typical subarray.

Format 2:

For (variable; condition; expression)

{Statement}

Example:


Copy codeThe code is as follows:
[Chengmo @ localhost nginx] # awk 'In in {
Total = 0;
For (I = 0; I <= 100; I ++)
{
Total + = I;
}
Print total;
}'
5050

3. do loop

Format:
Do
{Statement} while (condition)

Example:


Copy codeThe code is as follows:
[Chengmo @ localhost nginx] # awk 'In in {
Total = 0;
I = 0;
Do
{
Total + = I;
I ++;
} While (I <= 100)
Print total;
}'
5050

The above is an awk flow control statement. we can see from the syntax above that it is the same as the C language. With these statements, many shell programs can be handed over to awk, and the performance is very fast.

Break When a break statement is used for a while or for statement, the program exit loop occurs.
Continue When a continue statement is used for a while or for statement, the program is moved to the next iteration cyclically.
Next Can read the next input line and return to the top of the script. This avoids other operations on the current input row.
Exit Statement to exit the main input loop and transfer the control to the END, if the END exists. If no END rule is defined or the exit statement is applied to the END, the script is executed.
   

III. performance comparison

[Chengmo @ localhost nginx] # time (awk 'In in {total = 0; for (I = 0; I <= 10000; I ++) {total + = I ;} print total ;}')
50005000

Real 0m0. 003 s
User 0m0. 003 s
Sys 0m0. 000 s
[Chengmo @ localhost nginx] # time (total = 0; for I in $ (seq 10000); do total = $ ($ total + I); done; echo $ total ;)
50005000

Real 0m0. 141 s
User 0m0. 125 s
Sys 0m0. 008 s

To implement the same function, we can see that awk achieves 50 times the performance of 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.