Linux Shell variables Mathematical operations

Source: Internet
Author: User
Tags arithmetic ibase stdin

Linux Shell variables Mathematical operations

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.

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:
The "expr" command also supports the $ symbol reference variable for basic operations, but a space must be used as a delimiter between the variable and the operator;
After you have used the "expr" command to operate on a variable, the entire expression must use the "· Expression "Mode is assigned to the variable, which is included in the" ' "character,
and is 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 ...

Floating-point arithmetic is not supported in the four kinds of shells described above that perform variable operations!

2, the Linux Shell uses the BC Command for advanced math operations:

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:
The calculation accuracy of BC can be specified by additional parameters.
Additional parameters use the semicolon ";" as the delimiter;
Result2= ' echo ' scale=9; $no/3; "| BC ';
echo "RESULT2 = $result 2";


As shown in the experimental results, use the semicolon ";" to add the 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 the power operation of value;
echo "sqrt" = ' echo ' sqrt (100) "| BC ' ";
echo "sqrt" = $ (echo "sqrt (100)" | BC) ";
echo "10^3 = ' echo ' 10^3" | BC ' ";
echo "10^3 = $ (echo" 10^3 "| BC) ";

      • This article is from: Linux Learning Tutorial Network

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.