Although Bash is a scripting language, it has pretty much all the
capabilities of a general purpose programming language. This includes
arithmetic functions.
There are a number of syntax options you can use to evoke arithmetic
evaluation of an expression. Perhaps the most readable one is the let command.
For example
let "m = 4 * 1024"
will compute 4 times 1024 and assign the result to the variable "m".
You can print out the result by adding an echo statement:
let "m = 4 * 1024"
echo $m
You can test this from the command line by entering the following code:
let "m = 4 * 1024"; echo $m
You can also create a file containing the Bash commands, in which case
you should add a line at the top of the file that specifies the program
that is supposed to execute the code. For example:
#!/bin/bash
let "m = 4 * 1024"
echo $m
assuming the Bash executable is located in /bin/bash. You also need to set the permissions of your script file so that it is executable. Assuming the script file name is script1.sh, you can set the permissions to make the file executable with the command:
chmod 777 script1.sh
After that you can execute it with the command:
./script1.sh
The available arithmetic operations are similar to those in standard
programming languages like Java and C. Besides multiplication, as
illustrated above, you use addition:
let "m = a + 7"
or subtraction:
let "m = a - 7"
or division:
let "m = a / 2"
or modulo (the remainder after an integer division):
let "m = a % 100"
When an operation is applied to the same variable to which the result is
assigned you can use the standard arithmetic shorthand assignment
operators, also referred to as compound assignment operators. For
example, for addition we have:
let "m += 15"
which is equivalent to "m = m + 15". For subtraction we have:
let "m -= 3"
which is equivalent to "m = m - 3". For division we have:
let "m /= 5"
which is equivalent to "m = m / 5". And for modulo, we have:
let "m %= 10"
which is equivalent to "m = m % 10".
Additionally, you can use the increment and decrement operators:
let "m++"
is equivalent to "m = m + 1". And
let "m--"
is equivalent to "m = m - 1".
And then there is the ternary "question mark-colon" operator, which
returns one of two values depending on whether the specified condition
is true or false. For example
let "k = (m < 9) ? 0 : 1"
The right hand side of this assignment statement evaluates to "0" if the
variable "m" is less than 9. Otherwise it evaluates to 1. This means
the variable "k" is assigned "0" if "m" is less than 9 and "1"
otherwise.
The general form of the question mark-colon operator is:
condition ? value-if-true : value-if-false
Floating Point Arithmetic in Bash
The let operator only works for integer arithmetic. For floating point arithmetic you can use for example the GNU bc calculator as illustrate in this example:
echo "32.0 + 1.4" | bc
The "pipe" operator "|" passes the arithmetic expression "32.0 + 1.4" to the bc calculator, which returns the real number. The echo command prints the result to the standard output.
Alternative Syntax for Arithmetic
Backticks (back single quotes) can be used to evaluate an arithmetic expression as in this example:
echo `expr $m + 18`
This will add 18 to the value of the variable "m" and then print out the result.
To assign the compute value to a variable you can use the equal sign without spaces around it:
m=`expr $m + 18`
Another way to evaluate arithmetic expressions is to use double parenthesis. For example:
(( m *= 4 ))
This will quadruple the value of the variable "m".
Besides arithmetic evaluation, the Bash shell provides other programming constructs, such as for-loops, while-loops, conditionals, and functions and subroutines.
0 comments:
Post a Comment