0

I am making a testing script to compile (via make) and run a bunch of tests. I have a series of cat, grep and sed commands that organizes the results of all the tests nicely, however core-dumps are the exception. Let's say I have a program called test. If ./test core dumps, the following lines will not catch it (as in it will not be piped to the file):

./test 2>&1 > >(tee log)
./test > log 2>&1

It's probably because of this error message from the script:

./script.sh: line 53: 4783 Aborted (core dumped) ./test 2>&1 > >(tee log)

I have a hacky workaround that makes make launch the program with a custom rule:

make run-test 2>&1 | tee -a log

This currently works, but it's a bit hacky having to use the makefile to do this. Is there another way to capture the core-dump without aborting the shell script line?

1

If you’re willing to run your test program from another script, it should be good enough to capture the output from that script.  I.e., put “./test” into testscript.sh, and then say

./testscript.sh > log 2>&1

A more compact but less intuitive solution is

sh -c "./test" > log 2>&1

Both solutions are based on the fact that the shell that runs a program is the guy who notices when the program dumps core, and issues the corresponding error message.  Therefore, to capture that message, you must capture the output from the shell.

2

It is the shell that prints that message rather than the crashing program. To capture the output of the shell as well, you want:

{
./test
} > log 2 >&1

The braces cause a sub shell to be run to execute the commands inside, and the output of the entire sub shell is redirected instead of just the one command.

Alternatively if you want all output redirected for the remainder of the script, you can use:

exec > log 2>&1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.