Linux shell variable mathematical operations
Abstract:
1) using the let, [], () Operators in Linux shell to operate shell variables for simple basic operations;
2) use expr and bc in Linux shell to implement advanced operations;
1. Basic operations on Linux shell Variables
A value is directly assigned to a variable as a regular variable and saved as a string.
The 1.1 let command can be used to directly execute basic operations:
When I use let, we do not use the $ symbol to reference variables.
No1 = 7;
No2 = 8;
Echo "------------ let command -------------"
Let no1 ++;
Let no2 --;
Let no1 + = 3;
Let no2-= 5;
Let result = no1 + no2;
Printf "let result = % d \ n" $ result;
1.2 "[]" operator is very similar to the let command: "[]" operator can use the $ symbol to reference a variable without spaces.
Echo "----------------- [] operator ----------------"
Printf "no1: % d no2: % d \ n" $ no1 $ no2;
Result1 = $[no1 + no2];
Printf "result1 = % d \ n" $ result1;
Result2 = $[no1 + no2 + 7];
Printf "result2 = % d \ n" $ result2;
Result3 = $ [$ no1 + $ no2 + 5];
Printf "result3 = % d \ n" $ result3;
No2 = $[no2 + 1];
Printf "no1 = % d no2 = % d \ n" $ no1 $ no2;
1.3 "()" operator is the same as "[]" OPERATOR:
You can also use the $ symbol to reference a variable for basic operations, without spaces between the variable name and the operator.
Echo "----------------- () operator --------------"
Printf "no1: % d no2: % d \ n" $ no1 $ no2;
Result1 = $(no1 + no2 ));
Printf "result1 = % d \ n" $ result1;
Result2 = $(no1 + no2 + 3 ));
Printf "result2 = % d \ n" $ result2;
Result3 =t3 ($ no1 + $ no2 + 5 ))
Printf "result3 = % d \ n" $ result3;
The 1.4 "expr" command can also be used for basic operations on variables:
The "expr" command also supports the basic operation of $ symbol referenced variables, but spaces must be used between variables and operators as separators;
After you use the "expr" command to calculate a variable, the entire expression must use the "· expression ·" mode to assign a value to the variable, that is, contained in the "'" character,
And is equivalent to the "$ (expression)" mode.
Echo "---------------- expr command ---------------"
Printf "no1: % d no2: % d \ n" $ no1 $ no2;
Result1 = 'expr 3 + 4 ';
Printf "result1 = % d \ n" $ result1;
Result2 = 'expr $ no1 + 4 ';
Printf "result2 = % d \ n" $ result2;
Result3 = 'expr $ no1 + $ no2 ';
Printf "result3 = % d \ n" $ result3;
Result4 = 'expr $ no1 + $ no2 ';
Printf "result4 = % d \ n" $ result4;
Result5 =$ (expr $ no1 + 3 );
Printf "result5 = % d \ n" $ result5;
Result6 =$ (expr $ no1 + 4 );
Printf "result6 = % d \ n" $ result6;
Result7 =$ (expr $ no1 + $ no2 );
Printf "result7 = % d \ n" $ result7;
As shown in the experiment results, in the expression of the "expr" command, spaces must be used between variables and operators as separators,
One thing I don't understand is why the expression in line 42 is wrong and the error is reported in line 43.
"Expr" also supports many computation expressions. You can run the expr -- help Command on the terminal to check whether o (partition _ tables) o haha...
Floating point operations are not supported in the above four shell variable operations!
2. Use the bc command in Linux shell to perform advanced mathematical operations:
The bc command uses stdin as the standard input;
Bc is an advanced calculator that supports precise floating-point operations;
Bc has many input options and supports mathematical function calls;
Run bc -- help to view the input options supported by bc;
The 2.1 bc command uses the standard input stdin as the input and supports floating point operations:
Echo "----------------- bc command ----------------"
Echo "4*0.56" | bc;
No = 48;
Result1 = 'echo "$ no * 1.5" | bc ';
Echo "result1 = $ result1 ";
The 2.2 bc command supports Operation precision settings:
You can use additional parameters to specify the operation precision for bc;
The semicolon ";" is used as the separator for additional parameters;
Result2 = 'echo "scale = 9; $ no/3;" | bc ';
Echo "result2 = $ result2 ";
As shown in the experiment results, use the Semicolon ";" to add the additional parameter scale, and specify the precision as 9 decimal places;
2.3 use bc to convert numeric values in hexadecimal notation:
Ibase = value is used as an additional parameter to specify the input variable's notation;
The obase = value is used as an additional parameter to specify the notation of the output variable;
No = 100;
Echo "echo \" obase = 2; ibase = 10; $ no \ "| bc = 'echo" obase = 2; ibase = 10; $ no "| bc '";
No = 1000;
Echo "echo \" scale = 5; obase = 10; ibase = 2; $ no/3 \ "| bc = 'echo" scale = 5; obase = 10; ibase = 2; $ no/3 "| bc '";
2.4 use bc to call mathematical formulas for advanced mathematical operations:
"Sqrt (value)" executes the Open-side operation of value;
"Value ^ index" performs the power operation of value;
Echo "sqrt (100) = 'echo" sqrt (100) "| bc '";
Echo "sqrt (100) = $ (echo" sqrt (100) "| bc )";
Echo "10 ^ 3 = 'echo" 10 ^ 3 "| bc '";
Echo "10 ^ 3 = $ (echo" 10 ^ 3 "| bc )";