Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
Returning values from a C++ program into a bash script

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.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda