如何在 Linux shell 中对变量进行除法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18093871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:29:04  来源:igfitidea点击:

How can I do division with variables in a Linux shell?

linuxbashshellvariablesdivision

提问by Judking

When I run commands in my shell as below, it returns an expr: non-integer argumenterror. Can someone please explain this to me?

当我在我的 shell 中运行如下命令时,它返回一个expr: non-integer argument错误。有人可以向我解释一下吗?

$ x=20
$ y=5
$ expr x / y 
expr: non-integer argument

采纳答案by paddy

Those variables are shell variables. To expand them as parameters to another program (ieexpr), you need to use the $prefix:

这些变量是外壳变量。要将它们作为参数扩展到另一个程序(expr),您需要使用$前缀:

expr $x / $y

The reason it complained is because it thought you were trying to operate on alphabetic characters (ienon-integer)

它抱怨的原因是因为它认为您正在尝试对字母字符(非整数)进行操作

If you are using the Bash shell, you can achieve the same result using expression syntax:

如果您使用的是 Bash shell,则可以使用表达式语法获得相同的结果:

echo $((x / y))

Or:

或者:

z=$((x / y))
echo $z

回答by Todd A. Jacobs

Referencing Bash Variables Requires Parameter Expansion

引用 Bash 变量需要参数扩展

The default shell on most Linux distributions is Bash. In Bash, variables must use a dollar sign prefix for parameter expansion. For example:

大多数 Linux 发行版上的默认 shell 是 Bash。在 Bash 中,变量必须使用美元符号前缀进行参数扩展。例如:

x=20
y=5
expr $x / $y

Of course, Bash also has arithmetic operatorsand a special arithmetic expansion syntax, so there's no need to invoke the exprbinary as a separate process. You can let the shell do all the work like this:

当然,Bash 还具有算术运算符和特殊的算术扩展语法,因此无需将expr二进制文件作为单独的进程调用。你可以让 shell 做这样的所有工作:

x=20; y=5
echo $((x / y))

回答by zielonys

I believe it was already mentioned in other threads:

我相信它已经在其他线程中提到了:

calc(){ awk "BEGIN { print "$*" }"; }

then you can simply type :

然后你可以简单地输入:

calc 7.5/3.2
  2.34375

In your case it will be:

在您的情况下,它将是:

x=20; y=3;
calc $x/$y

or if you prefer, add this as a separate script and make it available in $PATH so you will always have it in your local shell:

或者,如果您愿意,请将其添加为单独的脚本并使其在 $PATH 中可用,这样您将始终在本地 shell 中拥有它:

#!/bin/bash
calc(){ awk "BEGIN { print $* }"; }

回答by zielonys

Why not use let; I find it much easier. Here's an example you may find useful:

为什么不使用 let ;我觉得容易多了。这是一个您可能会觉得有用的示例:

start=`date +%s`
# ... do something that takes a while ...
sleep 71

end=`date +%s`
let deltatime=end-start
let hours=deltatime/3600
let minutes=(deltatime/60)%60
let seconds=deltatime%60
printf "Time spent: %d:%02d:%02d\n" $hours $minutes $seconds

Another simple example - calculate number of days since 1970:

另一个简单的例子 - 计算自 1970 年以来的天数:

let days=$(date +%s)/86400

回答by Pooja Rajput

let's suppose

让我们假设

x=50
y=5

then

然后

z=$((x/y))

this will work properly . But if you want to use / operator in case statements than it can't resolve it. enter code hereIn that case use simple strings like div or devide or something else. See the code

这将正常工作。但是,如果您想在 case 语句中使用 / 运算符,则无法解决它。 在此处输入代码在这种情况下,使用简单的字符串,如 div 或 devide 或其他东西。看代码

回答by Shayan Chatterjee

To get the numbers after decimal point, you can do this:-

要获得小数点后的数字,您可以这样做:-

read num1 num2
div=`echo $num1 / $num2 |bc -l`
echo $div