Temporarily redirecting stderr in bash script

I have a simple script that is used to run another program. This other program can sometimes give SIGSEGV , which violates my conclusion. So I added a couple of lines that should temporarily redirect stderr to /dev/null so that SIGSEGV ignored. Below is the code of my code:

 exec 2> /dev/null progname >& ./tmp/run.txt && run_status='OK' exec 2>1 

The problem is that the last line does not do what I want. Obviously, the first line works and redirects stderr . The last line should return stderr back to where it used to be (which I assumed is the same as stdout ).

Any help would be assigned!

+4
source share
2 answers

Another variant:

 exec 3> /dev/stderr 2> /dev/null progname >& ./tmp/run.txt && run_status='OK' exec 2>&3 

Or even

 exec 3>&2 2> /dev/null progname >& ./tmp/run.txt && run_status='OK' exec 2>&3 

Thus, the script maintains the separation of stdout and stderr for the script (i.e. stdout and stderr scripts can be redirected separately.

+8
source

Why not just redirect it just for run?

  progname > ./tmp/run.txt 2>/dev/null && run_status='OK' 

Or maybe

 { progname > ./tmp/run.txt && run_status='OK' } 2>/dev/null 
0
source

All Articles