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
I've also tried replacing the last line with: printf "DESCBC average is: " $avgDESCBC >> results.txt
the outtime.txt is:
0.220
0.218
0.226
0.223
0.217
and results.txt is:
DES3 average is: 0
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)?
EDIT: 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).
Try this script. Using { , } around time command you can capture the OUTPUT of "time" command and use "2" identifier for creating outtime.txt file in append mode. Before starting the script, this file should be created fresh OR you can comment the ">outtime.txt" line. A ';' character just before closing '}' brace is important OR it won't end the begining '{'. This will FIX your outtime.txt file contents/data issue.
Other issue is with while loop as you are using "|" before while loop and due to that a new subshell is getting created for while loop and tsum variable is loosing its value when it's out of the while loop. Feed while loop "outtime.txt" file like shown below in my reply.
OUTPUT:
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.This happens because you are creating a new subprocess when you use the
|
operator just beforewhile
, 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.This is done by changing this
to this
With this simple change, your new output becomes
To learn more, read here.
http://www.gnu.org/software/bash/manual/html_node/Redirections.html