I have a bash script that runs a simulation program written in Fortran 90, and all output is redirected to a file. If the program finishes without problems, I set a success parameter. The code looks something like this:
#!/bin/bash ... echo -n 'Running program...' ./sim_program >& file && success='true' if [ $success ]; then echo 'OK' else echo 'NOT OK' fi ...
The output to screen should be either ‘Running program… OK’ or ‘Running program… NOT OK’. In some cases, the simulation program will crash with a floating point exception or a segmentation fault, and the corresponding signals are sent (SIGSEGV / SIGFPE). The output may then look something like this:
:~>execute_script Running program.../path/to/script: line 232: 15350 Floating Point Exception ./sim_program >& file && success='true' NOT OK
How can I suppress the error output due to the SIGFPE or SIGSEGV such that I get
:~>execute_script Running program... NOT OK
even when there is such an error? I have looked into using trap, e.g.
trap '' SIGSEGV SIGFPE ./sim_program >& file && success='true' trap SIGSEGB SIGFPE
but then I still get something like
:~>execute_script Running program... Floating Point Exception NOT OK
Any help is appreciated!
That error message is probably going to stderr.
Try putting this at the start of your script:
and anything send to stderr will go to the null device rather than your terminal.