I'd like to compile and run a C++ program via Bash and then capture the output produced by cout (which will be a int) to a variable in the Bash script.
I'm currently able to direct the output to a text file like so:
./prog >> output.txt
My research has led me to this, which appears to be creating an empty string:
output=$(./prog) | bc -l
In case it's relevant, I'm trying to capture this value so that I can average the output of my program over multiple executions. Here's my entire script as it currently stands:
count=1
while [ $count -le 8192 ]
do
sum=0
arraysize=$(( $count * 1024 ))
g++-7 prog.cpp -DGLOBAL=${arraysize} -DLOCAL=32 -o prog -lm -fopenmp -framework OpenCL
for try in {0..9}
do
output=$(./prog) | bc -l
echo ${output} # this line appears to print an empty string
sum=$(( (sum + output) ))
done
echo ${sum / 10}
count=$(( $count * 2 ))
done
I know that there are likely other errors in the script above, but the one that's currently hanging me up is just capturing the value produced by the program.
./prog >> output.txt...bc? Maybe try to move that into the parenthesis, if you actually want to capture the outputbcgives for the output of your program.typeset -i outputto declare the variable to hold an integer.