如何从 unix csh/tcsh shell 执行基本算术

新手上路,请多包涵

在 windows 下,当我需要进行基本计算时,我会使用内置计算器。现在我想知道如果你只有一个外壳,常见的方法是什么。

谢谢

原文由 vehomzzz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 497
2 个回答

你总是可以使用 python 解释器,它通常包含在 linux 发行版中。

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

等号 (‘=’) 用于为变量赋值。之后,在下一个交互式提示之前没有显示结果:

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

当然还有 数学 模块可以解决你的大部分计算器需求。

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

原文由 monkut 发布,翻译遵循 CC BY-SA 2.5 许可协议

这个网页(对于 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 方法。

原文由 dmckee — ex-moderator kitten 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题