I have a script that is rather "noisy". I've been asked to clean up the output so that most of the script's output goes to a log file, and the user just sees the start-up message, and the message when the script ends. To do this, I'm using exec like this:
#! /bin/bash echo "Welcome! set up has begun, this may take a few moments..." exec 3>&1 4>&2 1>> script.log 2>&1 # Call a bunch of other programs - stderr and stdout must # be written to a log file to diagnose programs, but nothing out the console... exec 1>&3 2>&4 echo "Your system is ready to use!"
I'm concerned though, about what will happen if one of the programs inside the logged portion of this script fails (I don't have much control over these programs) and causes the main script to fail. If the line
exec 1>&3 2>&4 fails to execute, I'm worried the user could end up with stderr and stdout redirected, leaving their terminal unusable! Is there a safer way to handle this?