Abstract:
1) The Linux shell uses let, [], (()) Three operators to manipulate shell variables for simple basic operations;
2) The Linux shell uses expr and BC two programs to achieve advanced operations;
1, basic operation of Linux shell variables
Values are assigned directly to variables as regular variables and are saved as strings.
The 1.1 Let command can be used to perform basic operations directly:
When I make use of let, we do not use the $ symbol to reference the variable.
- 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;
The 1.2 "[]" operator is similar to the Let command:
the "[]" operator can refer to a variable using the $ symbol, while supporting no spaces between the variable name and the operator.
- echo "-----------------[] operator----------------"
- printf "no1:%d no2:%d \ n" $no 1 $no 2;
- RESULT1=$[No1 + NO2 ];
- printf "result1 =%d \ n" $result 1;
- RESULT2=$[No1+no2 + 7];
- printf "result2 =%d \ n" $result 2;
- RESULT3=$[$no 1+$no 2+5 ];
- printf "RESULT3 =%d \ n" $result 3;
- NO2=$[NO2 + 1 ];
- printf "No1 =%d No2 =%d \ n" $no 1 $no 2;
The 1.3 "(())" operator is the same as the "[]" operator:
It is also supported to use the $ symbol reference variable for basic operations , while supporting the variable name with no spaces between the operators .
- echo "-----------------(()) operator--------------"
- printf "No1:%d No2:%d \ n" $no 1 $no 2;
- RESULT1=$((No1 + NO2 ));
- printf "result1 =%d \ n" $result 1;
- RESULT2=$((No1+no2+3));
- printf "result2 =%d \ n" $result 2;
- RESULT3=$(($no 1+$no 2 + 5))
- printf "RESULT3 =%d \ n" $result 3;
The
1.4 "expr" command can also be used for basic operations of variables:
However, a space must be used as a delimiter between the variable and the operator,
in use "E xpr "command after the operation of the variable, the entire expression must use"
•
expression
• "mode assigns a value to the variable, which is included in the
" Character,
and equivalent to "$ (
expression "mode.
- echo "----------------expr command---------------"
- printf "No1:%d No2:%d \ n" $no 1 $no 2;
- RESULT1=' Expr 3 + 4 ';
- printf "result1 =%d \ n" $result 1;
- Result2=' expr $no 1 + 4 ';
- printf "result2 =%d \ n" $result 2;
- RESULT3=' expr $no 1 + $no 2 ';
- printf "RESULT3 =%d \ n" $result 3;
- Result4=' expr $no 1+$no 2 ';
- printf "Result4 =%d \ n" $result 4;
- RESULT5=$(expr $no 1 + 3);
- printf "RESULT5 =%d \ n" $result 5;
- Result6=$(expr $no 1+4);
- printf "result6 =%d \ n" $result 6;
- Result7=$(expr $no 1+$no 2);
- printf "result7 =%d \ n" $result 7;
As shown in the experimental results, the expression in the "expr" command must use a space between the variable and the operator as a delimiter.
One thing I don't understand is why the 42-line expression has been wrong, and why it was in line 43.
"Expr" also supports a number of arithmetic expressions, everyone in the terminal to play the expr--help command to see it, O (∩_∩) o haha ...
The
four types of shells described above
floating-point arithmetic is not supported in the way that variable operations are performed!
2, the Linux Shell uses the BC command to achieve advanced math Operation:
The BC command uses the standard input stdin as input;
BC is a high-level calculator that supports accurate floating-point arithmetic;
BC has a considerable number of input options, and supports mathematical function calls;
Perform BC--HELP self-View the input options supported by BC;
The 2.1 BC command uses the standard input stdin as input and supports floating-point operations:
- echo "-----------------BC command----------------"
- Echo "4 * 0.56" | BC;
- No=48;
- RESULT1=' echo ' $no * 1.5 " | BC";
- echo "RESULT1 = $result 1";
The 2.2 BC command supports the Operation Precision setting:
Additional parameters use semicolons ";" as separators;
- result2= echo "scale =9; $no/3; " | BC '
- echo
Experiment results, using semicolons ";"
add additional parameter scale , specifying a precision of 9 decimal places;
2.3 Use BC to convert a numeric value between binary conversions:
By Ibase=value as additional parameters, the input variables are specified in the decimal notation;
By Obase=value as an additional parameter, specify the input 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 invoke mathematical formulas for advanced math operations:
"sqrt (value)" Performs the root operation of value;
"value^ Index "Performs a power operation of value;
- echo " sqrt (+) = ' echo ' sqrt (100 ) "| BC ' "
- echo "sqrt = $ (echo" Sqrt 100) "| BC) "
- echo "10^3 = ' echo ' 10^3" | BC ' "
- echo "| BC) "
Http://blog.chinaunix.net/uid-20671208-id-3552751.html
Linux Shell variables Mathematical operations