bash expr:非整数参数。如何减去点十进制数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22626932/
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
expr: non-integer argument. How to subtract dot-decimal numbers
提问by Coenster
I am trying to subtract two numbers by running the following bash script:
我试图通过运行以下 bash 脚本来减去两个数字:
#!/bin/bash -x
cur_length=`cat length.txt`
cur_pos=`cat pos.txt`
diff=`$(expr $cur_length - $cur_pos)`
echo "$diff"
But the output says expr has some issue:
但是输出说 expr 有一些问题:
+++ expr 235.68 - 145.9
expr: non-integer argument
+ diff=
+ echo ''
I have searched for "expr: non-integer argument" on the net, but nothing involves dot-decimal numbers. How can I subtract numbers like this? 235.68 - 145.9
我在网上搜索过“expr:非整数参数”,但没有涉及点十进制数。我怎样才能减去这样的数字?235.68 - 145.9
Thanks in advance.
提前致谢。
回答by John1024
Bash doesn't do fractions, just integers. Use bc
instead:
Bash 不做分数,只做整数。使用bc
来代替:
$ echo '235.68 - 145.9' | bc
89.78
That result can, of course, be put in a shell variable the same way that you were doing with expr
:
当然,该结果可以以与您相同的方式放入 shell 变量中expr
:
$ diff="$(echo '235.68 - 145.9' | bc)"
$ echo $diff
89.78