Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [91]

By Root 968 0
data: we just need one simple awk step and a sort step to make the data look like something that we already can handle!

Simple Math: expr

The expr command is one of the few Unix commands that is poorly designed and hard to use. Although standardized by POSIX, its use in new programs is strongly discouraged, since there are other programs and facilities that do a better job. In shell scripting, the major use of expr is for shell arithmetic, so that is what we focus on here. Read the expr(1) manpage if you're curious about the rest of what it can do.

expr's syntax is picky: operands and operators must each be separate command-line arguments; thus liberal use of whitespace is highly recommended. Many of expr's operators are also shell metacharacters, so careful quoting is also required.

expr is designed to be used inside of command substitution. Thus, it "returns" values by printing them to standard output, not by using its exit code ($? in the shell).

Table 7-7 lists expr's operators, in order of increasing precedence. Operators with the same precedence are grouped together.

Table 7-7. expr operators

Expression

Meaning

e1 | e2

If e1 is nonzero or non-null, its value is used. Otherwise, if e2 is nonzero or non-null, its value is used. Otherwise, the final value is zero.

e1 & e2

If e1 and e2 are non-zero or non-null, the return value is that of e1. Otherwise, the final value is zero.

e1 = e2

Equal.

e1 != e2

Not equal.

e1 < e2

Less than.

e1 <= e2

Less than or equal to.

e1 > e2

Greater than.

e1 >= e2

Greater than or equal to.

These operators cause expr to print 1 if the indicated comparison is true, 0 otherwise. If both operands are integers, the comparison is numeric; otherwise, it's a string comparison.

e1 + e2

The sum of e1 and e2.

e1 - e2

The difference of e1 and e2.

e1 * e2

The product of e1 and e2.

e1 / e2

The integer division of e1 by e2 (truncates).

e1 % e2

The remainder of the integer division of e1 by e2 (truncates).

e1 : e2

Match of e1 to BRE e2; see the expr(1) manpage for details.

( expression )

The value of expression; used for grouping, as in most programming languages.

integer

A number consisting only of digits, although an optional leading minus sign is allowed. Sadly, unary plus is not supported.

string

A string value that cannot be mistaken for a number or an operator.

In new code, you can do almost all of these operations using either test or $((...)). Regular-expression matching and extraction can be done with sed or the shell's case statement.

Here is an example of simple arithmetic. In a real script, the loop body would do something worthwhile, instead of just printing the loop variable's value:

$ i=1

Initialize counter

$ while [ "$i" -le 5 ]

Loop test

> do

> echo i is $i

Loop body: real code goes here

> i=`expr $i + 1`

Increment loop counter

> done

i is 1

i is 2

i is 3

i is 4

i is 5

$ echo $i

Show final value

6

This kind of arithmetic represents 99% of the use of expr that you are likely to encounter. We've purposely shown the use of test (in its alias as [...]) and backquotes for command substitution, since that is how expr is typically used. In new code, you should use the shell's built-in arithmetic substitution:

$ i=1

Initialize counter

$ while [ "$i" -le 5 ]

Loop test

> do

> echo i is $i

Loop body: real code goes here

> i=$((i + 1))

Increment loop counter

> done

i is 1

i is 2

i is 3

i is 4

i is 5

$ echo $i

Show final value

6

For whatever it's worth, expr supports 32-bit arithmetic, and on many systems, 64-bit arithmetic. Thus, there is little danger of counter overflow.

Quoting

Quoting is how you prevent the shell from interpreting things differently from what you want it to. For example, if you want a command to receive an argument containing metacharacters, such as * or ?, you have to quote the metacharacters. Or, quite typically, when you want to keep something as a single argument

Return Main Page Previous Page Next Page

®Online Book Reader