conditional redirection in bash

For bash, you can use the line:

exec &>/dev/null

This will direct all stdout and stderr to /dev/null from that point on. It uses the non-argument version of exec.

Normally, something like exec xyzzy would replace the program in the current process with a new program but you can use this non-argument version to simply modify redirections while keeping the current program.

So, in your specific case, you could use something like:

tty -s
if [[ $? -eq 1 ]] ; then
    exec &>/dev/null
fi

If you want the majority of output to be discarded but still want to output some stuff, you can create a new file handle to do that. Something like:

tty -s
if [[ $? -eq 1 ]] ; then
  exec 3>&1 &>/dev/null
else 
  exec 3>&1
fi
echo Normal               # won't see this.
echo Failure >&3          # will see this.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)