Fortune Telling Collection - Comprehensive fortune-telling - How to input calculator at command line under Linux?

How to input calculator at command line under Linux?

Linux command line calculator

Please ask man bc for the detailed documents.

Under windows, everyone knows that you can open a calculator by running calc directly (c:\windows\system32\calc.exe).

note:

Calculate vt. & intransitive verbs

Calculation; Calculator Calc is short for this kind of calculation.

Ca stands for calcium in chemistry. Calcium?

Calcium (element 20, symbol Ca). What is the connection between the two?

Calculate comes from calculus, which means pebbles for arithmetic operation, and is short for calx (limestone).

So under the linux system, is there a calculator similar to calc.exe under windows?

The following summarizes three commands under linux to introduce the calculation method under linux:

BC 1)

By default, Bc is an interactive instruction. In the bc working environment, you can use the following calculation symbols:

+addition

-Subtraction

* Multiplication

/division

index

Remaining percentage

For example:

#

B.C.

BC 1.06

Copyright 199 1- 1994, 1997,1998,2000 Free Software Foundation,

Company.

This is free software without any guarantee.

Please type "Warranty" for details.

3+6

& lt= addition

nine

4+2*3? & lt= addition, multiplication

10

(4+2)* 3 & lt; = addition, multiplication (priority)

18

4*6/8?

& lt= multiplication, division

three

10^3?

& lt= exponent

1000

18%5= remainder

3+4; 5*2; 5^2; 18/4?

& lt= Enter multiple calculations in one line and use; Separated.

seven

10

25

four

Exit & lt= exit.

# BC

BC 1.06

Copyright 199 1- 1994, 1997,1998,2000 Free Software Foundation,

Company.

This is free software without any guarantee.

Please type "Warranty" for details.

Proportion =3

& lt= Set the number of decimal places

1/3

.333

give up

The above is interactive calculation, so the result can be calculated directly without interaction.

A. use echo and | methods, such as:

# echo "(6+3)*2" |bc

18

# Echo 15/4 | BC

three

# echo " scale = 2; 15/4"

B.C.

3.75

# echo“3+4; 5*2; 5^2; 18/4"

B.C.

seven

10

25

four

Besides,

In addition to scale setting decimal places, bc also has ibase and obase for other binary operations.

For example:

//Output A7 in 16 as 10. Note that English can only be capitalized.

# echo " ibase = 16; A7 inch

B.C.

167

//Convert binary111111/to binary 10.

# echo " ibase = 2; 1 1 1 1 1 1 1 1"

B.C.

255

//The input is 16 and the output is binary.

# echo " ibase = 16; o base = 2; B5-A4 "

B.C.

1000 1

Bc has a supplement, which can also be found in bc-help: bc can be followed by a file name. For example:

# more calc.txt

3+2

4+5

8*2

10/4

# bc calc.txt

five

nine

16

2

2) Expression

Expr command can not only calculate addition, subtraction, multiplication and division, but also many expressions, all of which can calculate the results. However, it should be noted that when calculating addition, subtraction, multiplication and division, don't forget to use spaces and escapes. under

Surface directly with examples to introduce expr operation, such as:

# Expression 6+

3?

(with spaces)

nine

# expr? 2 \*

3?

(with escape symbol)

six

# Expression 14% 9

5?

# a=3

# expr

$a+5?

(No spaces)

3+5

# expr $a +

five

(Variable, with spaces)

eight

#

A=' Expression 4+2'

echo $a

six

# expr $a + 3

nine

In addition, expr is also very convenient for string operation (calculation), such as:

//String length

#? Expression length

"Yang Zhigang cublog.cn"

2 1

//Get the string from the location

# expr substr " yangzhi gang . Cu blog . cn "

1 1 1

Yang Zhigang

//the beginning of the string

# expr index

"Yang Zhigang. cublog.cn" cu

13

3) DC

Not many people will use dc to calculate, because dc is more complicated than bc, but it is similar and not difficult to make simple plans. Dc is a stack push operation and is interactive by default, but

You can also use echo and | to match the plan.

For example:

# dc

three

2+

p

five

4*

p

20

give up

# echo 3 2+ 4* p |dc

20

4) Echo

It is well known that echo is used to echo. It is also calculated in bc. In fact, echo can also perform simple calculations alone, such as:

# echo $((3+5))

eight

# echo $(((3+5)*2))

16

Echo can also calculate variables, such as:

# a= 10

# b=5

# echo $(($a+$b))

15

# echo $a+$b

10+5

# echo $a+$b |bc

15

//Calculate the date of the day before yesterday

#? Echo date

+%Y%m%d '

200908 13

#? echo `date +%Y%m%d`-2

200908 13-2

#? echo `date +%Y%m%d`-2

B.C.

200908 1 1

5)AWK

Awk can be used for calculation when processing files, and of course it can also be used for calculation alone, such as:

# awk ' BEGIN { a = 3+2; print

a} '

five

# awk ' BEGIN { a =(3+2)* 2; print

a} '

10

Awk supports common operators such as+(addition),-(subtraction), * (multiplication),/(division) or * * (power) and% (modulus).

Wait a minute. In addition, awk also provides some commonly used mathematical functions, such as sin(x), cos(x), exp(x), log(x),

Sqrt(x), rand (). With these operators and functions, you can directly perform some simple operations:

# echo | awk“{ print

8+6}'

14

# echo | awk“{ print

8/6}'

1.33333

# echo | awk“{ print

9%5}'

four

Please feel free to use.

Please ask questions if you have any questions.

Please adopt it in time if you are satisfied. Thank you.