How to do basic arithmetic from unix csh / tcsh shell

In windows, when I need to perform basic calculations, I use the built-in calculator. Now, I would like to know what is the common path if you only have a shell.

thanks

+3
source share
8 answers

And you can always use the python interpreter, which is usually included in Linux distributions.

http://docs.python.org/tutorial/introduction.html#using-python-as-a-calculator

$ python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>> # This is a comment
... 2+2
4
>>> 2+2  # and a comment on the same line as code
4
>>> (50-5*6)/4
5
>>> # Integer division returns the floor:
... 7/3
2
>>> 7/-3
-3
>>> # use float to get floating point results.
>>> 7/3.0
2.3333333333333335

An equal sign ('=') is used to assign a value to a variable. Subsequently, until the next interactive prompt, the result is not displayed:

>>> width = 20
>>> height = 5*9
>>> width * height
900

, , math, .

>>> import math
>>> math.pi
3.1415926535897931
>>> math.e
2.7182818284590451
>>> math.cos() # cosine
>>> math.sqrt()
>>> math.log()
>>> math.log10()
+1

- ( csh , ):

% @ x = (354 - 128 + 52 * 5 / 3)
% echo Result is $x
Result is 174

% set y = (354 - 128 + 52 / 3)
% echo Result is $y
Result is 354 - 128 + 52 / 3

.

/bin/sh awk - ( ) bash.

+13

dc. bc.

+9

, "" .

+8

Bash () $(( )):

$ echo $(( 100 / 3 ))
33
$ myvar="56"
$ echo $(( $myvar + 12 ))
68
$ echo $(( $myvar - $myvar ))
0
$ myvar=$(( $myvar + 1 ))
$ echo $myvar
57

( IBM)

+4
source

If you use bash, here is a handy example of a small script shell that allows you to perform calculations from the command line (including specifying the precision of floating point numbers):

http://www.novell.com/coolsolutions/tools/17043.html

0
source

You can also easily use Perl, where bc or expr are not powerful enough:

$ perl5.8 -e '$a=1+2; print "$a\n"' 
3
0
source

An alternative is to use the BC built-in command.

-1
source

Source: https://habr.com/ru/post/1717783/


All Articles