将float变量以十进制形式输出到BASH中的文件 [英] Output float variable as decimal to file in BASH

查看:62
本文介绍了将float变量以十进制形式输出到BASH中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BASH时间的结果(运行5次)以小数形式存储在文本文件中.然后,我读回值并使用bc计算平均值.最后,我将所得的平均值以小数形式输出到文件中.我的脚本似乎有效,(Linux Mint上的Mate Terminal中没有错误,这两个.txt文件都已创建),但文件的最终输出为"0".

The result of BASH time (run 5 times) is stored in a text file as decimal. I then read back in the values and compute the average using bc. Finally, I output the resulting average as a decimal to a file. My script seems to work, (no errors in Mate Terminal on Linux Mint, both .txt files are created) except the final output to file is "0".

TIMEFORMAT=%R
tsum=0

for i in {1..5}
do
(time sh -c \
'openssl des3 -e -nosalt -k 0123456789012345 -in orig.jpg -out encr.enc; '\
'openssl des3 -d -nosalt -k 0123456789012345 -in encr.enc -out decr.dec'\
) 2>&1 | grep 0 >> outtime.txt
done

avgDES3=0
cat outtime.txt | \
while read num
do
tsum=`echo $tsum + $num | bc -l`
done

avgDES3=`echo "$tsum / 5" | bc -l`
echo "DES3 average is: " $avgDES3 >> results.txt

我也尝试过将最后一行替换为:printf"DESCBC平均值为:" $ avgDESCBC >> results.txt

I've also tried replacing the last line with: printf "DESCBC average is: " $avgDESCBC >> results.txt

outtime.txt是:

the outtime.txt is:

0.220
0.218
0.226
0.223
0.217

,results.txt是:

and results.txt is:

DES3 average is:  0

我非常感谢您帮助将得到的平均值设为小数.也许我没有在最后一行的下一行中使用tsum变量的正确值(例如,如果循环内的表达式未更改tsum global)?

I'd appreciate help getting the resulting average to be a decimal. Perhaps I'm not using the correct value of the tsum variable in the next to last line (eg. if tsum global isn't changed by the expression within the loop)?

问题(由rbong和Arun指出)正在传递到子shell(循环表达式后,全局变量未更改).原始脚本正在我的系统上生成适当的outtime.txt(没有错误,只是没有从循环中获取tsum值).

Issue (as pointed out by rbong and Arun) was piping to a subshell (global variable not changed after loop expression). Origninally script was producing appropriate outtime.txt on my system (no errors, just didn't get tsum value from loop).

推荐答案

使用 bash -x 选项执行脚本进行调试显示,tsum变量在while循环中按预期运行,然后循环退出后,其值重置为零.

Executing your script with the bash -x option for debugging reveals that the tsum variable is acting as expected in your while loop, then its value is reset to zero after the loop exits.

之所以发生这种情况,是因为在 while 之前使用 | 运算符时,您正在创建一个新的 subprocess ,并且该子进程具有自己的副本的变量.您可以通过不将cat的输出管道到循环中,而使用重定向运算符来实现相同的结果而不创建子进程来避免这种情况.

This happens because you are creating a new subprocess when you use the | operator just before while, and the subprocess has its own copy of the variable. You can avoid this by not piping the output from cat into a loop, but instead using a redirect operator to achieve the same result without creating a subprocess.

这可以通过更改

cat outtime.txt | \
while read num
do
tsum=`echo $tsum + $num | bc -l`
done

对此

while read num
do
    tsum=`echo $tsum + $num | bc -l`
done < outtime.txt

通过此简单的更改,您的新输出将变为

With this simple change, your new output becomes

DES3 average is:  .22080000000000000000

要了解更多信息,请在此处阅读.

To learn more, read here.

http://www.gnu.org/software/bash/manual/html_node/Redirections.html

这篇关于将float变量以十进制形式输出到BASH中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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