How to use awk flow Control statement of shell in linux
Editor to share with you how to use shell awk process control statements in linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
one。 Conditional judgment statement (if)
The code is as follows:
If (expression) # if (Variable in Array)
Statement 1
Else
Statement 2
In the format, "statement 1" can be multiple statements, and if you want to make it easier for Unix awk to judge and facilitate your own reading, you'd better enclose multiple statements in {}. The Unix awk branching structure allows nesting in the following format:
The code is as follows:
If (expression)
{statement 1}
Else if (expression)
{statement 2}
Else
{statement 3}
The code is as follows:
[chengmo@localhost nginx] # awk 'BEGIN {
Test=100
If (test > 90)
{
Print "very good"
}
Else if (test > 60)
{
Print "good"
}
Else
{
Print "no pass"
}
}'
Very good
Each command statement can be followed by a ";" sign.
two。 Loop statement (while,for,do)
1.while statement
Format:
While (expression)
{statement}
Example:
The code is as follows:
[chengmo@localhost nginx] # awk 'BEGIN {
Test=100
Total=0
While (I)