Previous Section  < Day Day Up >  Next Section

8.3. Arithmetic

Arithmetic is not built into the Bourne shell. If you need to perform simple integer arithmetic calculations, the UNIX expr command is most commonly used in Bourne shell scripts. For floating-point arithmetic, the awk or bc programs can be used. Because arithmetic was not built in, the performance of the shell is degraded when iterating through loops a number of times. Each time a counter is incremented or decremented in a looping mechanism, it is necessary to fork another process to handle the arithmetic.

8.3.1 Integer Arithmetic and the expr Command

The expr command is an expression-handling program. When used to evaluate arithmetic expressions, it can perform simple integer operations (see Table 8.2). Each of its arguments must be separated by a space. The +, , *, /, and % operators are supported, and the normal programming rules of associativity and precedence apply.

Example 8.5.

1   $ expr 1 + 4

    5

2   $ expr 1+4

    1+4

3   $ expr 5 + 9  /  3

    8

4   $ expr 5 * 4

    expr: syntax error

5   $ expr 5 \* 4  -  2

    18

6   $ expr 11 % 3

    2

7   $ num=1

    $ num=`expr $num + 1`

    $ echo $num

    2


Table 8.2. The expr Command Arithmetic Operators

Operator

Function

*

Multiplication

/

Division

%

Modulus

+

Addition

Subtraction


EXPLANATION

  1. The expr command evaluates the expression. The two numbers are added.

  2. Because there are no spaces between the operator, the expression is evaluated as a string.

  3. Addition and division are combined. The division is performed first and then the addition.

  4. The asterisk (*) is evaluated by the shell as one of its wildcards, causing the expr command to fail.

  5. The asterisk (*) is escaped with a backslash to prevent shell interpretation. The expr command performs arithmetic.

  6. The modulus operator (%) returns the remainder after division is performed.

  7. The variable num is assigned 1. The expr command adds 1 to the value of the variable and assigns the result to num. The value of num is echoed to the screen.

8.3.2 Floating-Point Arithmetic

The bc, awk, and nawk utilities are useful if you need to perform more complex calculations.

Example 8.6.

(The Command Line)

1   $ n=`echo "scale=3; 13 / 2" | bc`

    $ echo $n

    6.500

2   $ n=`bc << EOF

    scale=3

    13/2

    EOF`

    $ echo $n

    6.500

3   $ product=`nawk -v x=2.45 -v y=3.123 'BEGIN{printf "%.2f\n",x*y}'`

    $ echo $product

    7.65


EXPLANATION

  1. The output of the echo command is piped to the bc program. The scale is set to 3, which is the number of significant digits to the right of the decimal point that will be printed. The calculation is to divide 13 by 2. The entire pipeline is enclosed in backquotes. Command substitution will be performed and the output assigned to the variable n.

  2. This example uses the here document within backquotes to perform the same function as the first example. The output of the command will be asssigned to the variable n and then displayed with the echo command.

  3. The nawk program gets its values from the argument list passed in at the command line, x=2.45 y=3.123. (The –v switch works with nawk, not awk.) After the numbers are multiplied, the printf function formats and prints the result with a precision of two places to the right of the decimal point. The output is assigned to the variable product.

    Previous Section  < Day Day Up >  Next Section