Linux Shell variables Mathematical operations

Source: Internet
Author: User
Tags arithmetic ibase stdin

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.

  1. No1=7;
  2. NO2=8;

  3. echo "------------let command-------------"
  4. Let No1++;
  5. Let No2--;
  6. Let No1+=3;
  7. Let No2-=5;
  8. Let result=no1+no2;
  9. 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.

  1. echo "-----------------[] operator----------------"
  2. printf "no1:%d no2:%d \ n" $no 1 $no 2;
  3. RESULT1=$[No1 + NO2 ];
  4. printf "result1 =%d \ n" $result 1;
  5. RESULT2=$[No1+no2 + 7];
  6. printf "result2 =%d \ n" $result 2;
  7. RESULT3=$[$no 1+$no 2+5 ];
  8. printf "RESULT3 =%d \ n" $result 3;
  9. NO2=$[NO2 + 1 ];
  10. 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 .
  1. echo "-----------------(()) operator--------------"
  2. printf "No1:%d No2:%d \ n" $no 1 $no 2;
  3. RESULT1=$((No1 + NO2 ));
  4. printf "result1 =%d \ n" $result 1;
  5. RESULT2=$((No1+no2+3));
  6. printf "result2 =%d \ n" $result 2;
  7. RESULT3=$(($no 1+$no 2 + 5))
  8. 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.

  1. echo "----------------expr command---------------"
  2. printf "No1:%d No2:%d \ n" $no 1 $no 2;
  3. RESULT1=' Expr 3 + 4 ';
  4. printf "result1 =%d \ n" $result 1;
  5. Result2=' expr $no 1 + 4 ';
  6. printf "result2 =%d \ n" $result 2;
  7. RESULT3=' expr $no 1 + $no 2 ';
  8. printf "RESULT3 =%d \ n" $result 3;
  9. Result4=' expr $no 1+$no 2 ';
  10. printf "Result4 =%d \ n" $result 4;
  11. RESULT5=$(expr $no 1 + 3);
  12. printf "RESULT5 =%d \ n" $result 5;
  13. Result6=$(expr $no 1+4);
  14. printf "result6 =%d \ n" $result 6;
  15. Result7=$(expr $no 1+$no 2);
  16. 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:
    1. echo "-----------------BC command----------------"
    2. Echo "4 * 0.56" | BC;
    3. No=48;
    4. RESULT1=' echo ' $no * 1.5 " | BC";
    5. echo "RESULT1 = $result 1";


The 2.2 BC command supports the Operation Precision setting:

Additional parameters use semicolons ";" as separators;

  1. result2= echo "scale =9; $no/3; " | BC '
  2. 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;

  1. No=100;
  2. echo "echo \" obase=2;ibase=10; $no \ "| bc = ' echo ' obase=2; ibase=10; $no"| BC ' ";
  3. No=1000;
  4. 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;

    1. echo " sqrt (+) = ' echo ' sqrt (100 ) "| BC ' "
    2. echo "sqrt = $ (echo" Sqrt 100) "| BC) "
    3. echo "10^3 = ' echo ' 10^3" | BC ' "
    4. echo "| BC) "




Http://blog.chinaunix.net/uid-20671208-id-3552751.html

Linux Shell variables Mathematical operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.