将值从C ++程序返回到bash脚本

时间:2021-01-19 00:07:11

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:

我有一个计算双重结果的C ++程序(在Linux上),我想写一个bash脚本,它运行程序的次数可变,并为我平均这些结果。为简单起见,请考虑以下代码:

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.

有没有一种简单的方法可以将程序的结果传回bash脚本?我读到了关于使用退出代码,但返回类型是一个双,所以我认为这不会起作用。从字符串输出中解析值是不实用的,因为程序具有其他终端输出。

2 个解决方案

#1


9  

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

UNIX的方法是在stderr上写入非必要数据并在stdout上写入实际结果。这样,你就可以做到

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.

请注意,您的shell脚本有几个语法错误。尝试使用shellcheck自动整理其中的许多内容,并随时发布有关您无法解决的问题的问题。

#2


3  

Why don't you use return value as data to your bash script?

为什么不将返回值用作bash脚本的数据?

int main() {
        return 46;
}

The output is as follows (yes, it's bash script):

输出如下(是的,它是bash脚本):

./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

#1


9  

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

UNIX的方法是在stderr上写入非必要数据并在stdout上写入实际结果。这样,你就可以做到

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.

请注意,您的shell脚本有几个语法错误。尝试使用shellcheck自动整理其中的许多内容,并随时发布有关您无法解决的问题的问题。

#2


3  

Why don't you use return value as data to your bash script?

为什么不将返回值用作bash脚本的数据?

int main() {
        return 46;
}

The output is as follows (yes, it's bash script):

输出如下(是的,它是bash脚本):

./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