View and vote on the article here: Shell Programming Series (VI)
Shell Programming Series (VI)| Category | | | Summary | | Time for arithmetic. I would appreciate if you will recall your knowledge in arithmetic. Although the original Bourne shell has no built-in arithmetic handlers, arithmetic can still be done using command substitution along with the expr command. |
| | Body | Arithmetic in Shell Programs
For example:
var=`expr var1 + var2`
This adds the values contained in var1 and var2 together and then stores the result in var. Entering floating point numbers will cause an error. In addition, changing the order of operations with parentheses is not supported. Division that does not return an integer value will have decimal portion dropped. For example, 5/2 is 2 as far as expr is concerned. If you wish to retrieve the remainder of a division, you can use the modulus operator (%). For example, expr 5 % 2 will return 1.
Characters that have special meaning to the shell must be escaped. For example, expr 2 * 2 will not work because the shell interprets the * as a wildcard operator. For the operation to work, the multiplication operator (*) must be escaped like this: expr 2 * 2. This protects it from the shell and prevents the shell from giving it special meaning.
expr can also evaluate true/false expressions. If the expression is true, expr returns a 1. If the expression is false, expr returns 0. For example:
expr 2+2=4+3 // returns 0
expr 2+2=1+3 // returns 1
!= will reverse the equation. It means ?is not equal to.? So, for example, the equation expr 5!=3 will return 1 (true).
expr can of course, also evaluate less than/greater than expressions. Once again, because the characters have special meaning to the shell, they must be escaped when used with expr to protect them from the shell. It is a true/false evaluation just like the equal to operator. If the expression is true, expr returns 1. If the expression is false, expr returns 0. For example, expr 5 > 4 returns 1(true). In addition, greater than or equal to (>=) and less than or equal to (1. #!/bin/sh
2. pi=?3.14159265? # Assign the value of pi to a variable.
3. echo
4. echo ?This program computes both the circumference and the area of?
5. echo ?a circle?
6. echo
7. echo ?n ?Please enter the radius of the circle: ?
8. read radius
9. circumference= `echo ?$radius*$pi? | bc ?l`
10. area= `echo ?$radius^2*$pi? | bc ?l`
11. printf ? The circumference is: $circumference ?
12. printf ?The area is: $area ?
13. exit 0[/b]
Most of the concepts used in this program have already been explained, so rather than go through the program line by line, I am only going to cover the concepts that are new and/or important to the operation of the program.
? Line 2: This line assigns a reasonably accurate value of pi to a variable called pi. I point this out because it shows good programming practice. We could just as easily have typed the number 3.14159265 each time we needed to use pi in the program. But assigning the number to avariable serves two primary purposes. First, it simply makes the program more readable. Second, it makes the program much easier to maintain. If we later decided that we needed more precision for the value of pi, we would only have to change one number where we assign the value to the variable pi. If we had used the actual number each time in the program when we needed it, we would have to find and change each occurrence of pi when we decided we need more precision.
? Line 9: Line 9 uses command substitution and a pipe to compute the circumference of the circle and store the value in the variable circumference. Since bc does not accept expressions directly on the command line, we need to use the echo command to echo the expression we want computed and then pipe the output of echo to bc.
? Line 10: This line is similar to line 9. The caret (^) in the expression is the bc exponent operator. In this case, it raises the value stored in $radius by the power of 2. We do not need to use parentheses to change the order of operation because, as you may recall from algebra, exponents have higher precedence than multiplication. Therefore, we can be assured that the exponent will be evaluated before the multiplication is done.
? Lines 11 and 12: These lines display the results to the user. I used printf here because it gives me a nicer layout than echo. The use of the hardware tabs with the causes the numbers to line up in a nice column.
This is only a very simple use of bc. bc is quite powerful and can do much more than what I have shown here. As I said before, bc is a programming language in itself. See the man page for bc if you are interested in learning more about its capabilities, both inside shell scripts and in standalone programs.
|
|
This article was imported from zZine. (original author: ismail)
There are no replies to this post yet.
|