I have a C++ program (on Linux) that computes a double result, and I want to write a bash script that runs the program a variable number of times and averages these results for me. For simplicity, consider the following code:
main.cpp:
int main() {
cout << "Some other stuff\n";
double result = foo();
return 0;
}
script.sh:
sum = 0
num = $1
for((i = 0; i < $num; i++)); do
result = ./a.out; #store the result somehow?
sum = $sum + $result
done
avg = $sum / $num
echo "Average: " $avg
Is there an easy way to pass the result of the program back into the bash script? I read about using the exit code, but the return type is a double so I don't think that will work. Parsing the value from string output is unwieldy because the program has other terminal output.
The UNIX way of doing this is writing non-essential data on stderr and writing the actual result on stdout. That way, you can simply do
int main() {
cerr << "This is output that won't be captured." << endl;
cout << "3.141592" << endl;
}
and use command substitution to capture it:
result=$(./a.out)
An uglier way of doing that doesn't require changing the output is to write to another file descriptor:
int main() {
char* foo = "3.141592\n";
write(42, foo, strlen(foo));
}
This will let you capture the output with:
result=$(./a.out 42>&1 > /dev/null)
Note that your shell script has several syntax errors. Try shellcheck to automatically sort out many of them, and feel free to post a question about the issues you can't resolve.
Why don't you use return value as data to your bash script?
int main() {
return 46;
}
The output is as follows (yes, it's bash script):
./a.out ; echo $?
46
In case of double values you could use this approach:
#include <iostream>
int main() {
double res = 46.001;
std::cout << res << std::endl;
return 0;
}
And the output:
a=`./a.out`; echo $a
46.001
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With