gpt4 book ai didi

c++ - 如何将 C++ 输出捕获到 Bash 变量?

转载 作者:行者123 更新时间:2023-11-30 00:44:06 25 4
gpt4 key购买 nike

我想通过 Bash 编译和运行 C++ 程序,然后将 cout 产生的输出(这将是一个 int)捕获到 Bash 脚本中的一个变量。

我目前能够像这样将输出定向到文本文件:

./prog >> output.txt

我的研究使我想到了这一点,这似乎是在创建一个空字符串:

输出=$(./prog) | bc -l

如果它是相关的,我会 try catch 此值,以便我可以对多次执行的程序输出进行平均。这是我目前的整个脚本:

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

我知道上面的脚本中可能还有其他错误,但目前让我挂断的错误只是捕获程序产生的值。

最佳答案

以下是可以提供帮助的方法:

var=$(./prog)

关于c++ - 如何将 C++ 输出捕获到 Bash 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50634725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com