UNIX CSH/TCSH Shell에서 기본 산술을 수행하는 방법
문제
Windows에서 기본 계산을 수행해야 할 때 내장 계산기를 사용합니다. 이제 쉘 만있는 경우 일반적인 방법이 무엇인지 알고 싶습니다.
감사
해결책
And you can always use the python interpreter, it's normally included in linux distros.
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 math module 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()
다른 팁
에서 이 웹 페이지 (을 위한 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
접근하다.
여기에는 많은 좋은 솔루션이 있지만 쉘에서 산술을하는 '고전적인'방법은 Expr입니다.
세게 때리다 기본 (정수 만 해당) 산술을 지원합니다 내부에 $(( ))
:
$ echo $(( 100 / 3 ))
33
$ myvar="56"
$ echo $(( $myvar + 12 ))
68
$ echo $(( $myvar - $myvar ))
0
$ myvar=$(( $myvar + 1 ))
$ echo $myvar
57
(IBM 링크에서 직접 복사 된 예)
사용하는 경우 bash
, 다음은 명령 줄에서 계산할 수있는 작은 쉘 스크립트의 편리한 예입니다 (부동 소수점 번호의 정밀도 지정 포함).
BC 또는 EXPR이 충분히 강력하지 않은 경우 Perl을 쉽게 사용할 수 있습니다.
$ perl5.8 -e '$a=1+2; print "$a\n"'
3
대체 옵션은 내장 된 BC 명령을 사용하는 것입니다.