Expr command
========
At first, the Bourne shell provided a special command to process mathematical expressions. The expr command allows you to process mathematical expressions on the command line, but is particularly clumsy:
$ expr 1 + 5
6
The expr command can recognize different numbers and string operators, as shown in the following table:
========================================================== ====================================
Operator description
-----------------------------------------------------------------------
Arg1 | if no parameter is null or 0, arg1 is returned; otherwise, arg2 is returned.
If no parameter is null or 0, arg1 is returned; otherwise, 0 is returned.
Arg1 <arg2 if arg1 is smaller than arg2, 1 is returned; otherwise, 0 is returned.
Arg1 <= arg2 if arg1 is less than or equal to arg2, 1 is returned; otherwise, 0 is returned.
Arg1 = arg2 if arg1 is equal to arg2, 1 is returned; otherwise, 0 is returned.
Arg1! = Arg2 if arg1 is not equal to arg2, 1 is returned; otherwise, 0 is returned.
Arg1> = arg2 if arg1 is greater than or equal to arg2, 1 is returned; otherwise, 0 is returned.
Arg1> arg2 if arg1 is greater than arg2, 1 is returned; otherwise, 0 is returned.
Arg1 + arg2 returns the arithmetic operations and
Arg1-arg2 returns the arithmetic operation difference between arg1 and arg2.
Arg1 * arg2 returns the arithmetic product of arg1 and arg2.
Arg1/arg2 returns the Arithmetic Operators of arg1 and arg2.
Arg1 % arg2 returns the arithmetic remainder of arg1 and arg2
STR: EXP if exp matches a certain mode of STR, return this mode match
Match STR exp if exp matches a certain mode of STR, return this mode match
Substr str pos Len returns a string with a start position of pos (counting from 1) and a length of Len characters.
Index STR chars returns the location where the Chars string is located in STR; otherwise, 0 is returned.
Length STR returns the length of the STR value.
+ The token is interpreted as a string, even a keyword.
(Expression) returns the value of expression.
========================================================== ====================================
Although standard operators work well in expr commands, problems still occur when using them in scripts or command lines. Many expr command operators have other meanings in Shell (such as asterisks ). Using them in the expr command produces some strange results:
$ expr 5 * 2
expr: syntax error
$
To solve this problem, you need to use shell escape characters (backslash) to identify arbitrary characters that are easily interpreted by Shell errors before sending the expr command:
$ expr 5 \* 2
10
$
However, bash shell has an improvement in processing mathematical operators. See the following.
Square brackets
==========
BASH Shell includes the expr command to maintain compatibility with the Bourne shell, but it also provides a simpler way to execute mathematical expressions. In bash shell, when assigning a mathematical operation result to a variable, You can enclose the mathematical expression with a dollar sign and square brackets ($ [operation:
$ var1=$[1 + 5]
$ echo $var1
6
$ var2=$[$var1 * 2]
$ echo $var2
12
$
Using square brackets to execute shell mathematical operations is much more convenient than using expr commands. This technology can also work in shell scripts. Similarly, when using square brackets to calculate the formula, Do not worry that shell will misunderstand the multiplication or other symbols. Shell knows that it is not a wildcard because it is in square brackets.
However, bash shell supports only integer operations. If you want to calculate the floating point, go down and look at it.
Floating Point Solution
============
There are several solutions that can solve the integer limitation of mathematical operations in bash. The most common solution is to have a built-in bash calculator called BC.
### Basic BC usage
The bash calculator is actually a programming language that allows you to input floating-point expressions, explain expressions, computation, and return results on the command line. Bash calculator can recognize:
-Number (integer and floating point number)
-Variables (simple variables and arrays)
-Annotation (the line starting with # Or the/**/pair in C language)
-Expression
-Programming statement (for example, if-then)
-Function
You can access the bash calculator through the BC command at the shell prompt:
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty‘.
12*5.4
64.8
quit
Enter the formula. BC automatically returns the answer and exits the quit.
Floating point operations are controlled by a built-in variable called scale. You must set this value to the number of digits after the decimal point you want.
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty‘.
3.44 /5
0
scale=4
3.44 /5
.6880
quit
### Use BC in the script
You can use the back quotes or $ () to run the BC command and assign the output to a variable. The basic format is as follows:
variable = `echo "options; expression" | bc`
The first part of options allows you to set variables. If you need to set more than one variable, you can use semicolons to separate them. The expression variable defines the mathematical expressions executed through BC. Example:
#!/bin/bash
var1=`echo "scale=4; 3.44 / 5" | bc`
echo The answer is $var1
The result is: the answer is. 6880.
This method applies to shorter operations, but sometimes you will deal more with your own numbers. If you have a lot of operations, listing multiple expressions in the same command line will be a little troublesome.
There is a solution to this problem. The BC command recognizes input redirection and allows you to redirect a file to the BC command for processing. Then, this is also confusing, because you must store expressions in files.
The best way is to use inline input redirection, allowing you to redirect data directly on the console. In shell scripts, you can assign the output to a variable:
variable=`bc << EOF
options
statements
expressions
EOF
`
The EOF file string identifies the start and end of the inline redirection data. Remember that back quotes are still needed to assign the BC command output to the variable. For example:
#!/bin/bash
Var1 = 1, 10.46
Var2. = 43.67
Var3. = 33.2
Var4 = 71
Var5 = 'bc <EOF
Scale = 4
A1 = ($ var1 * $ var2)
B1 = ($ var3 * $ var4)
A1 + B1
EOF
`
Echo the final answer for this mass is $ var5