Home > OS >  Assign Valgrind's report to a variable in bash script
Assign Valgrind's report to a variable in bash script

Time:10-28

I'm working on a test suite for programs written in C. For this I made a bash script in which I run the submitted programs on all availabe test cases and compare their output to the expected output. As a last test case, I'd also like to do a check for memory leaks. The idea is to run Valgrind using the last availabe test case as input, and then assign Valgrind's output to a variable (discarding the program's output), which I would then use to look for certain erros using grep in order to output a summary in case some errors or leaks were indeed detected.

I've tried several things, but so far I'm unable to assign Valgrind's output to a variable.

Last thing I tried was:

TEST=$(valgrind ./a.out < "${infiles["$((len-1))"]}" >/dev/null)

I still get Valgrind's report displayed in the terminal and if I try to echo "$TEST" in the bash script, I get nothing.

CodePudding user response:

valgrind is writing its output to stderr, not stdout, but $(...) only captures stdout. So you have to redirect stderr to stdout.

TEST=$(valgrind ./a.out < "${infiles["$((len-1))"]}" 2>&1 >/dev/null)
  • Related