Shell Script Note (iii) mathematical calculations in the shell

Source: Internet
Author: User

Mathematical calculations in the shell

One, use square brackets
#!/bin/Basha=tenb=c=res=$[$a * ($c-$b)]  Echo $res
Ii. usage of BC

BC Technology is actually a programming language that recognizes:

numbers (integers and floating-point numbers)
Variable (simple variable and array)
Note (# c language /* */ start line)
expression
programming statements (e.g. if-then statement)
functions

floating-point operations are built-in variables Span class= "Fontstyle1" >scale controlled. You must set this value to the number of decimal
digits that you want to keep in the calculation results, otherwise you will not get the desired result.
/span>

$ BC-q3.4450scale =43.445. 6880 quit$

BC can also support variables:

$ BC-qvar1=45Print var2  2quit$

Use BC in scripts:

Way One:

variable=$ (echo "options; Expression "| bc

Instance:

#!/bin/bashvar1=$ (echo"scale=4; 3.44/5" | BC) Echo The answer is $var 1

Mode two, using inline input, format:

variable=$ (BC << EOF
Options
Statements
Expressions
Eof
)

var1=10.46var2=43.67var3=33.2var4=inVAR5 =$ (BC <<4= ($var 1 *= ($var 3 *+ b1eof)

Shell Script Note (iii) mathematical calculations in the shell

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.