Shell Scripting Raiders (learning notes)--1.6 math and BC commands

Source: Internet
Author: User
Tags arithmetic

This article directory:

1.6.1 Basic Integer Arithmetic

1.6.2 BC Command Advanced arithmetic operations

use Let, $ (()), or $[] for basic integer operations, using BC for advanced operations, including decimal operations. Where the expr command can also perform integer operations, and can determine whether the parameter is an integer, see the full solution of the expr command .

1.6.1 Basic Integer Arithmetic
[[email protected] tmp]# str=[[email protected] tmp]# let str=str+6  # equivalent to let str+=  6[[email protected] tmp]# let str-=5     # equivalent to let str=str-5Echo $str  One

If you want to do calculations on the command line, you can use $ (()) or $[].

[Email protected] ~]# str=Echo $ (str+=6)   echo $[str=str-6]

Of course, you can also use $ (()) and $[] when assigning arithmetic values to variables.

[Email protected] ~]# str=~]# str=$ ((str+=6)); Echo $str  -  ~]# str=$[str-=6]; Echo $str Ten

In fact, the variable on the right side of the equals sign can carry the $ symbol in the arithmetic calculation, but the variable on the left side of the equal sign is not allowed to take the $ symbol, because it is the variable to manipulate, not the reference variable. For example:

[Email protected] ~]# let str= $str-1# equivalent to let str=str-1[[Email protected]~]# str=$ ($str-1) # is equivalent to str=$ ((str-1)) [[email protected]~]# srt=$[$str-1] # equivalent to str=$[str-1][[email protected]~]#Echo$ ((str= $str-1) # is equivalent to echo $ ((str=str-1), but cannot be written as Echo $ ($str =str-1)) [[email protected]~]#Echo$[str= $str-1] # equivalent to echo $[str=str-1], but cannot be written as Echo $[$str =str-1]

You can also self-increment, subtract from the operation. "+ +" and "--" indicate that the variable is automatically added 1 and minus 1. But with different locations, the results returned are different.

X + +: Return results first, plus 1

++x: Add 1 First and return results

x--: Return results first, minus 1

--x: minus 1 before returning results

If the initial value of X is 10, echo $[x++] will display 10, but after the display (that is, after the result is returned), the value of X has become 11, and the Echo $x will return 11.

[Email protected] ~]# x=; Echo $ ((x + +)); Echo $x Ten  One

If Echo $[x++] still returns 11 at this point, the X is already 12.

Echo $ ((x + +)); Echo $x  One  A

The initial value of the x variable is initialized to 10, and Echo $[++x] displays 11, because the value of x is then displayed after adding 1 to the X,echo. ++x are completely equivalent to x=x+1, and they are all assigned by adding 1 first.

[Email protected] ~]# x=; Echo $ ((++x)); Echo $x  One  One

The same is true of self-subtraction.

Therefore, when you use self-increment or decrement to make variable assignments, you need to be aware that the assigned value is added and reduced immediately. For example:

[Email protected] ~]# x=;y=$ (x + +); echo $y; Echo $y Ten Ten

Because the value assigned to Y by y=$ (x + +) is the value before 1, although the assignment ends, $ (x + +) has become 11, but this is not related to Y.

Therefore, for the variable assignment of the self-increment class, you should use the method of "++x" or "--x" that first calculates the re-display.

[Email protected] ~]# x=;y=$ ((++x)); echo $y; Echo $y  One  One

To summarize the assignment operation of the numerical variables:

Let I=i-1

Let i= $i-1

Let I-=1

i=$ ((i-1))

i=$ (($i-1))

i=$[I-1]

i=$[$i-1]

echo $ ((i=i-1))

echo $ ((i= $i-1))

1.6.2 BC Command Advanced arithmetic operations

BC can be used to calculate floating-point numbers, which is a calculator in Linux.

Here is an example of a basic feature:

[[Email protected] ~]# BCB1.06. the# First output BC version information, you can use-q option does not output header information copyright1991-1994,197,1998, -,2004,2006Free software Foundation, Inc.this is Freesoftware with absolutely NO WARRANTY. For details type ' warranty'.Pie=3.1415# can assign a variable to a pie*3*3# No spaces required for operation28.2735R=3Pie*r*R28.2735Pie*r^2# You can use a power square28.2735R=3 /*set the radius to 3*/# You can also use C-style annotations

Enter the QUIT command to exit the BC calculator.

also supports self-increment and auto-decrement functions.

[[email protected] ~]# BC-qr=3R+ +3R+ +4+ +r6 + +r7--R6

The BC operator has an built-in variable scale that represents the precision of the calculation, with a default precision of 0, so the default result of the division operation is an integer.

/(1+3)3scale =3/(1+3  )3.250

A more user-friendly feature is the ability to use command substitution to implement batch mode calculations.

Its general format is referenced below:

Var= ' echo ' option1;option2; Expression "|BC"

Where the options section generally sets the precision scale, and the variable assignment, expression is the calculation of the expression, and finally put them in the inverse quotation marks assigned to the variable. Such as:

[Email protected] ~]# area= 'echo'scale=2;r=3;3.1415*r*r'|  echo  $area28.2735

Because it is specified on the command line, there are more restrictions on how to use it. BC accepts the use of here string and here document to receive parameters. The most common practice is to put them in a script.

#!/bin/ for calculate something var1=hahavar2=hehe Value= ' bc<< EOF  # uses here string in anti-quotesscale =3r=33.1415*r*REOF '  Echo $value

Here are a few different ways to calculate 1+2+...+10, which requires the output on the screen to be "1+2+3+4+5+6+7+8+9+10= calculation results", which is a good example.

 [[email protected] tmp]# echo  $ (seq -S  " +   " 10 " = '  1  +2  +3  +4  +5  + 6  +7  +8  +9  +10  = 55  
Echo $ (seq"+") =$ (('seq'+  "ten"))1+2+3+4+5+6 +7+8+9+Ten=
[Email protected] tmp]#Echo$(seq-S"+" Ten)=$(seq-S" + " Ten|Xargs Expr) # Note"+"And" + "1+2+3+4+5+6+7+8+9+Ten= -

Back to series article outline:http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprint Please specify source:http://www.cnblogs.com/f-ck-need-u/p/7231870.html

Shell Scripting Raiders (learning notes)--1.6 math and BC commands

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.