我正在寻找一种在终端中进行快速计算的方法(使用 Ubuntu 最低版本中可用的实用程序),并在数学语法不正确时获得准确的失败错误,例如:cli-calculator "foo+bar" || echo invalid math syntax
一些输入包括:
1+1 #应该打印 2 并返回 0 代码
5/2 #应该打印 2.5 并返回 0 代码
1/0 #应该返回非 0 代码
foo+bar #应该返回非 0 代码
我已经尝试过bc
,shell 集成算法,,,expr
和, awk
但到目前为止都没有完全满足我的要求,所以我正在寻找替代方案,请遵循用例:perl
python
bc
,它能够对浮点数进行运算,但不会在无效语法输入时返回错误代码$ echo "1/0" | bc -l && echo all is fine || echo math syntax error Runtime error (func=(main), adr=3): Divide by zero all is fine $ echo "foo+bar" | bc -l && echo all is fine || echo math syntax error 0 #should return an error all is fine
Shell 集成了算术和
expr
,不支持浮点数运算,并且在无效的数学输入上返回有效代码$ echo $((5/2)) 2 #should return 2.5 $ echo $((foo+bar)) 0 #should return an error $ expr 5 / 2 2 #should return 2.5 $ expr foo+bar foo+bar #should return an error
awk
|perl
,不要在无效的数学输入上返回无效的状态代码。$ awk "BEGIN {print foo+bar; exit}" 0 #should return a non 0 number and probably output an error $ echo "foo+bar" | perl -ple '$_=eval' 0 #should return a non 0 number and probably output an error
python
,支持浮点算术运算,并在无效的数学语法上返回状态错误,但速度很慢。$ python -c 'from __future__ import division; from math import *; print(foo+bar)' && echo all fine || echo math syntax error NameError: name 'foo' is not defined math syntax error #good! $ python -c 'from __future__ import division; from math import *; print(5/2)' && echo all fine || echo math syntax error 2.5` #good! all fine
有任何想法吗?
答案1
关于什么calc
sudo apt-get install apcalc
例子
% calc 1/0
Error 10001
% calc foo + bar
"foo" is undefined
Error in commands
% calc 5/2
2.5
或者qalc
% qalc 1/0
error: Division by zero.
1 / 0 = 1 / 0
% qalc foo + bar
error: "foo" is not a valid variable/function/unit.
0 + bar = 1 bar
% qalc 5/2
5 / 2 = 2.5
bc
echo "1/0" | bc
Runtime error (func=(main), adr=3): Divide by zero
echo "scale=1; 5/2" | bc
2.5
这是正确的,0 + 0 等于 0
% echo "foo+bar" | bc
0
或者, GNOME 桌面环境的计算器gcalccmd
的控制台版本。gnome-calculator
例子
% gcalccmd
> 1/0
Error (null)
> foo + bar
Error 3
> 5/2
2,5
请注意,gcalccmd
不支持 readline,因此只能使用最基本的行编辑功能。(Backspace 可用,但左/右键不可用)
答案2
八度是另一种选择。它是一个非常强大的免费工具,其语法“类似于商业工具 matlab”。如果您想在没有 GUI 的情况下运行它,请使用选项--no-gui
或命令octave-cli
。
sudo apt-get install octave octave-info
根据您的“有效和无效”表达的示例:
$ octave --no-gui
GNU Octave, version 4.0.0
Copyright (C) 2015 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
Octave was configured for "i686-pc-linux-gnu".
Additional information about Octave is available at http://www.octave.org.
Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html
Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.
>> 1+1 #valid
ans = 2
>> 5/2 #valid
ans = 2.5000
>> 1/0 #invalid
warning: division by zero
ans = Inf
>> foo+bar #invalid
error: 'foo' undefined near line 1 column 1
>> foo=1;bar=2 # assign values to foo and bar
bar = 2
>> foo+bar #now valid
ans = 3
>>