Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [134]

By Root 816 0
column 72.

Statements

Programming languages need to support sequential, conditional, and iterative execution. awk provides these features with statements borrowed largely from the C programming language. This section also covers the different statement types that are specific to awk.

Sequential Execution

Sequential execution is provided by lists of statements, written one per line, or separated by semicolons. The three lines:

n = 123

s = "ABC"

t = s n

can also be written like this:

n = 123; s = "ABC"; t = s n

In one-liners, we often need the semicolon form, but in awk programs supplied from files, we usually put each statement on its own line, and we rarely need a semicolon.

Wherever a single statement is expected, a compound statement consisting of a braced group of statements can be used instead. Thus, the actions associated with awk patterns are just compound statements.

Conditional Execution

awk provides for conditional execution with the if statement:

if (expression)

statement

1

if (expression)

statement

1

else

statement

2

If the expression is nonzero (true), then execute statement 1. Otherwise, if there is an else part, execute statement 2. Each of these statements may themselves be if statements, so the general form of a multibranch conditional statement is usually written like this:

if (expression

1)

statement

1

else if (expression

2)

statement

2

else if (expression

3)

statement

3

...

else if (expression

k)

statement

k

else

statement

k+1

The optional final else is always associated with the closest preceding if at the same level.

In a multibranch if statement, the conditional expressions are tested in order: the first one that matches selects the associated statement for execution, after which control continues with the statement following the complete if statement, without evaluating conditional expressions in the remainder of the statement. If no expressions match, then the final else branch, if present, is selected.

Iterative Execution

awk provides four kinds of iterative statements (loops):

Loop with a termination test at the beginning:

while (expression)

statement

Loop with a termination test at the end:

do

statement

while (expression)

Loop a countable number of times:

for (expr

1; expr

2; expr

3)

statement

Loop over elements of an associative array:

for (key in array)

statement

The while loop satisfies many iteration needs, typified by while we have data, process it. The do loop is much less common: it appears, for example, in optimization problems that reduce to compute an error estimate, and repeat while the error is too big. Both loop while the expression is nonzero (true). If the expression is initially zero, then the while loop body is not executed at all, whereas the do loop body is executed just once.

The first form of the for loop contains three semicolon-separated expressions, any or all of which may be empty. The first expression is evaluated before the loop begins. The second is evaluated at the start of each iteration, and while it is nonzero (true), the loop continues. The third is evaluated at the end of each iteration. The traditional loop from 1 to n is written like this:

for (k = 1; k <= n; k++)

statement

However, the index need not increase by one each iteration. The loop can be run backward like this:

for (k = n; k >= 1; k--)

statement

* * *

Tip


Because floating-point arithmetic is usually inexact, avoid for-statement expressions that evaluate to nonintegral values. For example, the loop:

$ awk 'BEGIN { for (x = 0; x <= 1; x += 0.05) print x }'...

0.85

0.9

0.95

does not print 1 in its last iteration because the additions of the inexactly represented value 0.05 produce a final x value that is slightly larger than 1.0.

* * *

C programmers should note that awk lacks a comma operator, so the three for loop expressions cannot be comma-separated lists of expressions.

The second form of the for loop is used for iterating over the

Return Main Page Previous Page Next Page

®Online Book Reader