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

veh*_*zzz 4 unix linux shell csh tcsh

在Windows下,当我需要执行基本计算时,我使用内置计算器.现在我想知道如果你只有一个shell,常见的方法是什么.

谢谢

dmc*_*kee 16

这个网页(为了csh和衍生品,因为你问):

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

% set y = (354 - 128 + 52 / 3)
% echo Result is $y
Result is 354 - 128 + 52 / 3
Run Code Online (Sandbox Code Playgroud)

注意不同的结果.

就个人而言,我坚持/bin/sh并致电awk或者某事(为了最大程度的便携性),或者其他人已经展示了这种bash方法.


alp*_*ero 10

你可以使用dc.或者bc.


Wil*_*ell 8

这里给出了许多好的解决方案,但在shell中进行算术运算的"经典"方法是使用expr.


mon*_*kut 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
Run Code Online (Sandbox Code Playgroud)

等号('=')用于为变量赋值。之后,在下一个交互式提示之前不会显示任何结果:

>>> width = 20
>>> height = 5*9
>>> width * height
900
Run Code Online (Sandbox Code Playgroud)

当然,还有数学模块,可以满足您的大部分计算器需求。

>>> import math
>>> math.pi
3.1415926535897931
>>> math.e
2.7182818284590451
>>> math.cos() # cosine
>>> math.sqrt()
>>> math.log()
>>> math.log10()
Run Code Online (Sandbox Code Playgroud)

  • 这个问题特定于 tcsh/csh。没有安装python的系统怎么办? (5认同)