Description: Bash has built-in support for integer arithmetic operations, but does not support floating-point arithmetic operations. the bc command can easily perform floating-point arithmetic operations. of course, the integer arithmetic operation is no longer needed. On the manual page, bc is Anarbitraryprecisioncalculatorlanguage, which is a description of any precision...
Bash has built-in support for four integer arithmetic operations, but does not support floating-point arithmetic operations. the bc command can easily perform floating-point computation. On the manual page, bc is An arbitrary precision calculator language, which is a computing language of any precision. Note that it is a language that provides some syntax structures, such as condition judgment and loops, it can be said that it is very powerful, but I have not found any occasion for this purpose. Another purpose is to perform hexadecimal conversion.
Common parameters
Generally, we use the bc command without any parameters.
Bc
If you want bc to output no prompt information, you can add the-q parameter:
Bc-q
If you want to use a powerful mathematical library, such as the calculation of trigonometric functions, you need to add the-l parameter:
Bc-l
Because bc itself is a command interpreter, to exit it, simply enter the quit press enter or press Ctrl + D to terminate.
Example
Example 1 use bc through the command line
[Root @ localhost centos39] # bc
Bc 1.06
Copyright 1991-1994,199 7, 1998,200 0 Free Software Foundation, Inc.
This is free software with absolutely no warranty.
For details type 'warranty '.
3 + 4
7
3-4
-1
3*4
12
3/4
0
Scale = 2; 3/4 # preserve decimal precision only for division, remainder, and power
. 75
3/4
. 75
3% 4
0
Scale = 0
3% 4
3
3 ^ 4
81
Ctrl + D
[Root @ localhost centos39] #
Example 2: MPs queue
[Root @ localhost centos39] # echo 3*4 | bc
(Standard_in) 1: parse error
[Root @ localhost centos39] # echo "3*4" | bc
12
[Root @ localhost centos39] # echo "scale = 7; 355/113" | bc
3.1415929
[Root @ localhost centos39] #
Example: Tri-hexadecimal conversion
[Root @ rhel55 ~] # Echo "ibase = 16; FFFF" | bc
65535
[Root @ rhel55 ~] # Echo "obase = 16; 1000" | bc
3E8
[Root @ rhel55 ~] #
We use the ibase and obase methods of bc.
Ibase is the base of the input number, while obase is the base of the output number. It is easy to remember that I is input and o is output.
If you use a command to convert numbers, you can use the echo command and pipeline to combine bc. As follows:
10 to 2:
$ Echo "obase = 2; ibase = 10; 100" | bc1100100
Hexadecimal to hexadecimal:
$ Echo "obase = 16; ibase = 10; 100" | bc64
Hexadecimal to hexadecimal:
$ Echo "ibase = 16; obase = 2; F1" | bc11110001
Note: the hexadecimal number F must be in upper case. if the lowercase result is incorrect. If no value is specified at the top, the default value is 10.
Example 4: write multiple expressions in one file for calculation
[Root @ rhel55 ~] # Cat test. bc
123*321
123/321
Size = 4; 123/321
[Root @ rhel55 ~] # Bc test. bc
Bc 1.06
Copyright 1991-1994,199 7, 1998,200 0 Free Software Foundation, Inc.
This is free software with absolutely no warranty.
For details type 'warranty '.
39483
0
. 3831
Ctrl + D
[Root @ rhel55 ~] #
[Root @ rhel55 ~] # Cat test. bc | bc
39483
0
. 3831
[Root @ rhel55 ~] #
Example 5 a Bash script used to calculate the Triangle area
First, review Junior High School knowledge: B indicates the bottom of the triangle, h indicates the height of the triangle, then the triangle area calculation formula is B * h/2.
File: area_of_triangle.sh
Bash Code
#! /Bin/bash
# Shell program/script to read the base and height of a traingle and find its area
#-------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project
# This script is licensed under gnu gpl version 2.0 or above
#-------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/for more information.
#-------------------------------------------------------------------------
# Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html
# Area = (1/2) x Base x Height
Echo-n "Enter base of a triangle :"
Read B
Echo-n "Enter height of a triangle :"
Read h
# Calculate it and display back
Area = $ (echo "scale = 2; (1/2) * $ B * $ h" | bc)
Echo "Area of a triangle is $ area"
[Root @ smsgw academic] #./area_of_triangle.sh
Enter base of a triangle: 123
Enter height of a triangle: 321
Area of a triangle is 19741.50
[Root @ smsgw academic] #
Example 6 use a script snippet of the bc command
Bash Code
# Usage: calc_sum
# Calculate the sum of two numbers
Calc_sum ()
{
Bc-q < $1 + $2
EOF
}
Paste the above code to the terminal.
[Root @ web ~] # Usage: calc_sum
[Root @ web ~] # Calculate the sum of two numbers
[Root @ web ~] # Calc_sum ()
> {
> Bc-q < > $1 + $2
> EOF
>}
[Root @ web ~] #
[Root @ web ~] # Usage: calc_free
[Root @ web ~] # Calculation fee, unit price: 0.05 yuan
[Root @ web ~] # Calc_extract ()
> {
> Bc-q < > 0.05*$1
> EOF
>}
[Root @ web ~] #
[Root @ web ~] #
[Root @ web ~] # Calc_sum 123 321
444
[Root @ web ~] # Calc_00001000
50.00
[Root @ web ~] #
Example 7 use a math library
In some articles, we can calculate the pi value of the circumference rate of 100 bits.
[Root @ web ~] # Echo "scale = 100; a (1) * 4" | bc
Runtime error (func = (main), adr = 11): Function a not defined.
[Root @ web ~] # Echo "scale = 100; a (1) * 4" | bc-l
3.141592653589793238462643383279502884197169399375105820974944592307 \
8164062862089986280348253421170676
[Root @ web ~] #
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.