如何在 Bash 脚本中添加数字?
我有这个 Bash 脚本,但在第 16 行遇到了问题。 我如何获取第 15 行的先前结果并添加 它到第 16 行的变量?
#!/bin/bash
num=0
metab=0
for ((i=1; i<=2; i++)); do
for j in `ls output-$i-*`; do
echo "$j"
metab=$(cat $j|grep EndBuffer|awk '{sum+=$2} END { print sum/120}') (line15)
num= $num + $metab (line16)
done
echo "$num"
done
I have this Bash script and I had a problem in line 16.
How can I take the previous result of line 15 and add
it to the variable in line 16?
#!/bin/bash
num=0
metab=0
for ((i=1; i<=2; i++)); do
for j in `ls output-$i-*`; do
echo "$j"
metab=$(cat $j|grep EndBuffer|awk '{sum+=$2} END { print sum/120}') (line15)
num= $num + $metab (line16)
done
echo "$num"
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
对于整数:
使用算术扩展:
$((EXPR))
使用外部
expr
实用程序。请注意,这仅适用于非常旧的系统。对于浮点:
Bash 不直接支持这一点,但是您可以使用一些外部工具:
您还可以使用科学计数法(例如,
2.5e+2)。
常见陷阱:
设置变量时,
=
两边不能有空格,否则会强制 shell 将第一个单词解释为变量的名称要运行的应用程序(例如,num=
或num
)num= 1
num =2
bc
和expr
期望每个数字和运算符作为单独的参数,因此空格很重要。它们无法处理像3+
+4
这样的参数。num=`expr $num1+ $num2`
For integers:
Use arithmetic expansion:
$((EXPR))
Using the external
expr
utility. Note that this is only needed for really old systems.For floating point:
Bash doesn't directly support this, but there are a couple of external tools you can use:
You can also use scientific notation (for example,
2.5e+2
).Common pitfalls:
When setting a variable, you cannot have whitespace on either side of
=
, otherwise it will force the shell to interpret the first word as the name of the application to run (for example,num=
ornum
)num= 1
num =2
bc
andexpr
expect each number and operator as a separate argument, so whitespace is important. They cannot process arguments like3+
+4
.num=`expr $num1+ $num2`
使用
$(( ))
算术展开式。有关详细信息,请参阅第 13 章算术扩展。
Use the
$(( ))
arithmetic expansion.See Chapter 13. Arithmetic Expansion for more information.
有一千零一种方法可以做到这一点。这是使用
dc
(一种逆波兰式桌面计算器,支持无限精度算术):但如果这对你来说太笨拙了(或者可移植性很重要),你可以说
但也许你是那些认为 RPN 令人讨厌和奇怪的人之一;不用担心!
bc
就在这里为您服务:也就是说,有一些您可以对脚本进行不相关的改进:
如 Bash FAQ 022 中所述,Bash 本身并不支持浮点数。如果您需要对浮点数求和,则需要使用外部工具(例如
bc
或dc
)。在这种情况下,解决方案是将
累积可能的浮点数添加到
num
中。There are a thousand and one ways to do it. Here's one using
dc
(a reverse Polish desk calculator which supports unlimited precision arithmetic):But if that's too bash-y for you (or portability matters) you could say
But maybe you're one of those people who thinks RPN is icky and weird; don't worry!
bc
is here for you:That said, there are some unrelated improvements you could be making to your script:
As described in Bash FAQ 022, Bash does not natively support floating point numbers. If you need to sum floating point numbers the use of an external tool (like
bc
ordc
) is required.In this case the solution would be
To add accumulate possibly-floating-point numbers into
num
.在 Bash 中,
请注意,Bash 只能处理整数算术,因此,如果您的 AWK 命令返回分数,那么您需要重新设计:这里是您的代码,稍微重写了一下,以便在 AWK 中完成所有数学运算。
In Bash,
Note that Bash can only handle integer arithmetic, so if your AWK command returns a fraction, then you'll want to redesign: here's your code rewritten a bit to do all math in AWK.
我总是忘记语法,所以我来到 Google 搜索,但我再也找不到我熟悉的语法了:P。这对我来说是最干净的,也更符合我对其他语言的期望。
I always forget the syntax so I come to Google Search, but then I never find the one I'm familiar with :P. This is the cleanest to me and more true to what I'd expect in other languages.
我也非常喜欢这个方法。混乱更少:
I really like this method as well. There is less clutter:
您应该将 metab 声明为整数,然后使用算术求值
有关详细信息,请参阅6.5 Shell 算术。
You should declare metab as integer and then use arithmetic evaluation
For more information, see 6.5 Shell Arithmetic.
使用 shell 内置
let
。它类似于(( expr ))
:来源:Bash let 内置命令
Use the shell built-in
let
. It is similar to(( expr ))
:Source: Bash let builtin command
另一种在 Bash 中执行的可移植的 POSIX 兼容方式,可以在
.bashrc
为所有方便的算术运算符。只需在命令行中调用它即可,
其想法是使用 Input-Field-Separator(IFS ),Bash 中的一个特殊变量,用于扩展后的单词拆分以及将行拆分为单词。该函数在本地更改值以使用分词字符作为求和运算符
+
。请记住,
IFS
是在本地更改的,并且不会对函数作用域外的默认IFS
行为生效。摘自man bash
页面,"$(( $* ))"
表示传递的参数列表,由+
分割,然后使用printf
输出总和值代码>函数。该函数还可以扩展以增加其他算术运算的范围。Another portable POSIX compliant way to do in Bash, which can be defined as a function in
.bashrc
for all the arithmetic operators of convenience.and just call it in command-line as,
The idea is to use the Input-Field-Separator(IFS), a special variable in Bash used for word splitting after expansion and to split lines into words. The function changes the value locally to use word-splitting character as the sum operator
+
.Remember the
IFS
is changed locally and does not take effect on the defaultIFS
behaviour outside the function scope. An excerpt from theman bash
page,The
"$(( $* ))"
represents the list of arguments passed to be split by+
and later the sum value is output using theprintf
function. The function can be extended to add scope for other arithmetic operations also.适用于 MacOS。 bc 是一个命令行计算器
Works on MacOS. bc is a command line calculator
这是我的解决方案
5.1
5.2
5.3
5.4
1
这是我的解决方案:
5.1
5.2
5.3
5.4
2
This is my solution
5.1
5.2
5.3
5.4
1
Here is my solution:
5.1
5.2
5.3
5.4
2
这是我的解决方案:
6.1
尾-n 15 ~/.bash_history | 64 位 | sha256sum
6.2
6.3
对于 {1..50} 中的 i; do echo“这是曼波第五号。”;完成> mambo_no_5.txt && openssl enc -aes-128-cbc -salt -in mambo_no_5.txt -out mambo_no_5.enc -k 你的密码
6.4
纳米 ~/.bashrc
导出 HISTSIZE=50
source ~/.bashrc
1
这是我的解决方案:
6.1
#!/bin/bash
6.2
#!/bin/bash
6.3
秀cu 别名
6.4
2
This is my solution:
6.1
tail -n 15 ~/.bash_history | base64 | sha256sum
6.2
6.3
for i in {1..50}; do echo "This is mambo no. 5."; done > mambo_no_5.txt && openssl enc -aes-128-cbc -salt -in mambo_no_5.txt -out mambo_no_5.enc -k yourpassword
6.4
nano ~/.bashrc
export HISTSIZE=50
source ~/.bashrc
1
Here is my solution:
6.1
#!/bin/bash
6.2
#!/bin/bash
6.3
Sau cu alias
6.4
2