Linux 如何从 unix csh/tcsh shell 执行基本算术
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1430395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to perform a basic arithmetics from unix csh/tcsh shell
提问by vehomzzz
Under windows, when I need to perform a basic calculations, I use a built-in calculator. Now I would like to find out what is the common way if you only have a shell.
在 Windows 下,当我需要执行基本计算时,我使用内置计算器。现在我想知道如果你只有一个shell,常用的方法是什么。
Thanks
谢谢
采纳答案by monkut
And you can always use the python interpreter, it's normally included in linux distros.
而且你总是可以使用 python 解释器,它通常包含在 linux 发行版中。
http://docs.python.org/tutorial/introduction.html#using-python-as-a-calculator
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
The equal sign ('=') is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:
等号 ('=') 用于为变量赋值。之后,在下一个交互提示之前不会显示任何结果:
>>> width = 20
>>> height = 5*9
>>> width * height
900
And of course there's the mathmodule which should solve most of your calculator needs.
当然还有数学模块可以解决您的大部分计算器需求。
>>> import math
>>> math.pi
3.1415926535897931
>>> math.e
2.7182818284590451
>>> math.cos() # cosine
>>> math.sqrt()
>>> math.log()
>>> math.log10()
回答by Mark Rushakoff
Bash supports basic (integer only) arithmeticinside $(( ))
:
击支持基本(整数只)算术内$(( ))
:
$ echo $(( 100 / 3 ))
33
$ myvar="56"
$ echo $(( $myvar + 12 ))
68
$ echo $(( $myvar - $myvar ))
0
$ myvar=$(( $myvar + 1 ))
$ echo $myvar
57
(example copied straight from the IBM link)
(示例直接从 IBM 链接复制)
回答by dmckee --- ex-moderator kitten
From this web page(for csh
and derivatives, since you asked):
从这个网页(对于csh
和衍生品,因为你问过):
% @ x = (354 - 128 + 52 * 5 / 3)
% echo Result is $x
Result is 174
and
和
% set y = (354 - 128 + 52 / 3)
% echo Result is $y
Result is 354 - 128 + 52 / 3
notice the different results.
注意不同的结果。
Personally, I stick to /bin/sh
and call awk
or something (for maximal portability), or others have exhibited the bash
approach.
就个人而言,我坚持使用/bin/sh
并调用awk
或其他方法(为了最大的可移植性),或者其他人已经展示了这种bash
方法。
回答by William Pursell
There are many good solutions given here, but the 'classic' way to do arithmetic in the shell is with expr.
这里给出了许多很好的解决方案,但在 shell 中进行算术运算的“经典”方法是使用 expr。
回答by DVK
You can also use Perl easily where bc or expr are not powerful enough:
您还可以在 bc 或 expr 不够强大的地方轻松使用 Perl:
$ perl5.8 -e '$a=1+2; print "$a\n"'
3
回答by Sachin Chourasiya
Alternative option is to use the built in BC command
另一种选择是使用内置的 BC 命令