bash 命令行终端上的乘法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11039876/
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
Multiplication on command line terminal
提问by bouncingHippo
I'm using a serial terminal to provide input into our lab experiment. I found that using
我正在使用串行终端为我们的实验室实验提供输入。我发现使用
$ echo "5X5"
just returns a string of "5X5"
. Is there a command to execute a multiplication operation?
只返回一串"5X5"
. 是否有执行乘法运算的命令?
回答by KurzedMetal
Yes, you can use bash's built-in Arithmetic Expansion $(( ))
to do some simple maths
是的,您可以使用bash 的内置算术扩展$(( ))
来做一些简单的数学运算
$ echo "$((5 * 5))"
25
Check the Shell Arithmetic section in the Bash Reference Manualfor a complete list of operators.
查看Bash 参考手册中的Shell 算术部分以获取完整的运算符列表。
For sake of completeness, as other pointed out, if you need arbitrary precision, bc
or dc
would be better.
回答by tvm
For more advanced and precise math consider using bc(1).
对于更高级和更精确的数学,请考虑使用 bc(1)。
echo "3 * 2.19" | bc -l
6.57
回答by Todd A. Jacobs
Internal Methods
内部方法
Bash supports arithmetic expansionwith $(( expression ))
. For example:
击支持算术扩展用$(( expression ))
。例如:
$ echo $(( 5 * 5 ))
25
External Methods
外部方法
A number of utilities provide arithmetic, including bcand expr.
许多实用程序提供算术,包括bc和expr。
$ echo '5 * 5' | /usr/bin/bc
25
$ /usr/bin/expr 5 \* 5
25
回答by William Pursell
The classical solution is:
经典的解决方案是:
expr 5 \* 5
Another nice option is:
另一个不错的选择是:
echo 5 5\*p | dc
Both of these solutions will only work with integer operands.
这两种解决方案都只适用于整数操作数。
回答by Moreaki
A simple shell function (no sed needed) should do the trick of interpreting '5X5'
一个简单的 shell 函数(不需要 sed)应该可以解释“5X5”
$ function calc { bc -l <<< ${@//[xX]/*}; };
$ calc 5X5
25
$ calc 5x5
25
$ calc '5*5'
25
回答by Paused until further notice.
I use this function which uses bc
and thus supports floating point calculations:
我使用这个函数,它使用bc
并因此支持浮点计算:
c () {
local a
(( $# > 0 )) && a="$@" || read -r -p "calc: " a
bc -l <<< "$a"
}
Example:
例子:
$ c '5*5'
25
$ c 5/5
1.00000000000000000000
$ c 3.4/7.9
.43037974683544303797
Bash's arithmetic expansion doesn't support floats (but Korn shell and zsh do).
Bash 的算术扩展不支持浮点数(但 Korn shell 和 zsh 支持)。
Example:
例子:
$ ksh -c 'echo "$((3.0 / 4))"'
0.75
回答by We Are All Monica
I have a simple script I use for this:
我有一个用于此的简单脚本:
me@mycomputer:~$ cat /usr/local/bin/c
me@mycomputer:~$ cat /usr/local/bin/c
#!/bin/sh
echo "$*" | sed 's/x/\*/g' | bc -l
It changes x
to *
since *
is a special character in the shell. Use it as follows:
它更改x
为*
因为*
是 shell 中的一个特殊字符。使用方法如下:
c 5x5
c 5-4.2 + 1
c '(5 + 5) * 30'
(you still have to use quotes if the expression contains any parentheses).
c 5x5
c 5-4.2 + 1
c '(5 + 5) * 30'
(如果表达式包含任何括号,您仍然必须使用引号)。
回答by bombs
If you like python and have an option to install a package, you can use this utilitythat I made.
如果你喜欢 python 并且可以选择安装一个包,你可以使用我制作的这个实用程序。
# install pythonp
python -m pip install pythonp
pythonp "5*5"
25
pythonp "1 / (1+math.exp(0.5))"
0.3775406687981454
# define a custom function and pass it to another higher-order function
pythonp "n=10;functools.reduce(lambda x,y:x*y, range(1,n+1))"
3628800