如何在bash中进行基本数学运算? [英] How can I do basic maths in bash?

查看:71
本文介绍了如何在bash中进行基本数学运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些我认为应该是名为bashispainful.sh的shell脚本:

I've made something that I think ought to be a shell script called bashispainful.sh:

代码:

#!/bin/bash
Var1= $((10*5))
Var2= $((10/5))
Var3= $((10-5))
Var4= $((10+5))
Var5= ($(Var4)*$(Var3))
echo $Var1
echo $Var2
echo $Var3
echo $Var4
echo $Var5

我想接收输出:

50
2
5
15
75

我当前的输出:

./Bashispainful.sh: line 2: 50: command not found
./Bashispainful.sh: line 3: 2: command not found
./Bashispainful.sh: line 4: 5: command not found
./Bashispainful.sh: line 5: 15: command not found
./Bashispainful.sh: line 6: syntax error near unexpected token `('
./Bashispainful.sh: line 6: `Var5= ($(Var4)*$(Var3))'

我的目标是掌握bash的基础数学知识.我已经尝试过每个网站,尝试过括号,美元符号及其缺乏的每种组合,但坦率地说,我感到困惑和绝望.如何在bash中执行简单的数学运算?

My aim here is to get any kind of grip on being able to do basic maths in bash. I have tried every website and every combination of brackets, dollar signs and lack thereof that I can think of and frankly I am baffled and desparate. How can I perform simple maths in bash?

(请使其易于理解-请假设我是非程序员且愚蠢)

(Please make this easy to understand - please assume that I am a non-programmer and stupid)

推荐答案

重击并不痛苦:

#!/bin/bash
Var1=$((10*5))
Var2=$((10/5))
Var3=$((10-5))
Var4=$((10+5))
Var5=$((Var4*Var3))
echo $Var1
echo $Var2
echo $Var3
echo $Var4
echo $Var5

输出为:

50
2
5
15
75

注释

考虑:

Var1= $((10*5))

bash执行算术替换后,该命令如下所示:

After bash does arithmetic substitution, that command looks like:

Var1= 50

该命令说(a)暂时将Var1设置为空,并且(b)运行名为50的命令.由于没有名为50的命令,您会收到错误:

That command says (a) temporarily set Var1 to empty, and (b) run the command named 50. Since there is no command named 50, you receive the error:

Bashispainful.sh: line 2: 50: command not found

解决方案是删除空格,以便将Var1分配给一个非空值:

The solution is remove the space so that the Var1 is assigned to a non empty value:

Var1=$((10*5))

考虑:

Var5= ($(Var4)*$(Var3))

对于名称以(开头的命令,这会将Var5临时设置为空.那是无效的.因此出现错误:

This sets Var5 temporarily to empty for a command whose name starts with (. That is not valid. Hence the error:

./Bashispainful.sh: line 6: syntax error near unexpected 

用于算术的正确bash语法为$((...)).因此,该行可以这样写:

The correct bash syntax for arithmetic is $((...)). Thus, that line could be written:

Var5=$(($Var4*$Var3))

但是,由于bash并不麻烦,因此可以理解算术表达式内的字符串是shell变量.因此,美元符号是不必要的,并且上面的内容可以简化为:

However, because bash is not painful, it understands that strings inside arithmetic expressions are shell variables. Thus, the dollar signs are unnecessary and the above can be simplified to:

Var5=$((Var4*Var3))

或者,由于上面只是对变量进行赋值,因此可以将其进一步简化为:

Alternatively, since the above is just doing assignment to variable, it can be further simplified to:

((Var5=Var4*Var3))

请注意,$((...))返回算术运算的值,但是((...))不返回任何值.它确实设置了退出代码,这使其对if语句中使用的测试有用.

Note that $((...)) returns the value of the arithmetic computation but ((...)) does not return any value. It does set an exit code which makes it useful for tests such as used in if statements.

这篇关于如何在bash中进行基本数学运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆