Linux Shell integer Operations Let [] (()) expr and floating-point BC usage

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 use let, we don't use the $ notation to reference variables.

  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:
The "expr" command also supports the $ symbol reference variable for basic operations, However, a space must be used as a delimiter between variables and operators;
After you have used the "expr" command to operate on a variable, the entire expression must use the "  ·  expression  ·"Mode assigns a value to a variable, which is included in the  " `  "Character Inside,
and is 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:

BC has three parameters, scale is the operation precision, ibase specifies the input variable of the notation, obase specify the output variable of the decimal system;
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:

The calculation precision of BC can be specified by additional parameters;
Additional parameters use the semicolon ";" as the delimiter;

  1. Result2= ' echo ' scale=9; $no/3; "| BC ';
  2. echo "RESULT2 = $result 2";


As shown in the experimental results, use the semicolon ";" 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 the 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 "10^3 = $ (echo" 10^3 "| BC) ";



Linux Shell integer Operations Let [] (()) expr and floating-point BC usage (RPM)

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.