Arithmetic Operations


If you have used other programming languages, you may expect to be able to include arithmetic operations directly in your shell scripts. For example, you might try to enter something like the following:

 $ x=2 $ x=$x+l $ echo $x 2+1

In this example, you can see that the shell concatenated the strings “2” and “+1” instead of adding 1 to the value of x. To perform arithmetic operations in your shell scripts, you must use the command expr.

The expr command takes a list of arguments, evaluates them, and prints the result on standard output. Each term must be separated by spaces. For example,

 $ expr 1 + 2 3

You can use command substitution to assign the output from expr to a variable. For example, you could increment the value of i with this line:

 i='expr $i + 1'

Drawbacks of expr

Unfortunately, expr is awkward to use because of collisions between the syntax of expr and that of the shell itself. You can use expr to add, subtract, multiply, and divide integers using the +, , *, and / operators. However, the * must be escaped with a backslash to prevent the shell from interpreting it as an asterisk:

 $ expr 5 + 6 11 $ expr 11 – 3 8 $ expr 8 / 2 4 $ expr 4 \* 4 16

Another drawback of expr is that it can only be used for integer arithmetic. If you try to give it a decimal argument, you will get an error, and it will truncate decimal results. For example,

 $ expr 1.5 + 2.5 expr: non-numeric argument $ expr 7 / 2 3

If you leave out the spaces between arguments, expr will not interpret your expression:

 $ expr 1+2 1+2

Other problems are that you cannot group arguments to expr with parentheses, and it does not recognize operations such as exponentiation. You can use the bc calculator, described in Chapter 19, to write scripts that can do these things. For example,

 echo "scale=2; (.5 + (7/2)) ^ 2" | bc

will print the number 16.00. Another way to address these problems is with the let command, which is included in ksh and bash.

Using let for Arithmetic

In bash and ksh, the let command is an alternative to expr that provides a simpler and more complete way to deal with integer arithmetic.

The following example illustrates a simple use of let:

 $ x=100 $ let y=2*(x+5) $ echo $y 210

Note that let automatically uses the value of a variable like x or y. You do not need to add a $ in front of the variable name.

The let command can be used for all of the basic arithmetic operations, including addition, subtraction, multiplication, integer division, calculating a remainder, and inequalities. It also provides more specialized operations, such as conversion between bases and bitwise operations.

You can abbreviate let statements with double parentheses, (( )). For example, this is the same as let x=x+3

 (( x = x+3 ))

Clearly, let is a significant improvement over expr. It still does not work with decimals, however, and it is not supported in sh. The limitations of expr and let are a good example of why shell is not the best language for some tasks.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net