Exec command - what is it doing here?

Hi all,
Forgive me for asking for help with my first post, but I am struggling here. I've been asked to translate a bash script into a Windows script (probably batch or powershell, not sure yet), so the first step is obviously understand what the bash script is doing. But I have no experience in bash, very little in unix/linux, and no unix box to practice on.
I can't figure out the following code:

hmdir=/home/CBG_VCC_POD/NEVIS/PROD/
timestamp=to_cbg_`date +%y%m%d_%H%M%S`

exec 1>"$hmdir"log/to_cbg_log/${timestamp}.log 2>"$hmdir"log/to_cbg_log/${timestamp}.err

if [ ! -d "$hmdir"Inbound ]; then
	echo "Inbound directory not found!!!" >> "$hmdir"log/to_cbg_log/${timestamp}.err
	echo "Exits..." >> "$hmdir"log/to_cbg_log/${timestamp}.err
	cp "$hmdir"log/to_cbg_log/${timestamp}.err "$hmdir"to_cbg/${timestamp}.err
	exit 1
fi

...

The first two lines obviously setup variables. Following the exec line, the script continues with various if loops to create folders and copy and zip files. So what is the exec line doing?

From what I read, exec starts a new process, executes the passed parameter and then exits, but in this case there is no passed parameter as far as I can tell, and the script does continue after this line.

A colleague postulated that it is setting the output for the following lines in this script to go to the log file in case of a command returning "1" and to an err file in the case of a command returning "2", or something like that. Can anyone clarify?

Many thanks in advance.
Nick

EXEC is used for below purpose

If command is supplied, it replaces the shell without creating a new process. If no command is specified, redirections may be used to affect the current shell environment.

If there are no redirection errors, the return status is zero; otherwise the return status is non-zero.

Hope it would help you to understand why they used exec in the script :slight_smile:

exec is a shell built-in. While it can be used to replace the current shell process with some other command, it is also used to manipulate file descriptors in the current shell. In this instance, since exec's arguments consist solely of redirections, no command is executed; exec is redirecting standard output, 1>, and standard error, 2>, to two different log files.

To experiment and learn, you can install Cygwin on your windows machine. The default install includes bash and other core utilities.

Regards,
Alister

Ok, so my colleague was more or less right. Any standard output from subsequent commands will go to the log file, and any standard error output from subsequent commands will go to the err file.
If I wanted to cancel this behaviour later in the script, how would I do that? Perhaps:

exec 1>nul 2>nul

?

Thanks for the Cygwin tip!

Depends what you mean by cancel.

If you simply want to discard output and error messages so that they cease to appear in the logs, you can redirect to /dev/null (although this will not affect any commands that are already logging and running).

If instead you would like to revert output and errors to their original destinations, you'll have to use exec to save the original locations.

The very last example at Advanced Bash-Scripting Guide: I/O Redirection demonstrates how to do this with standard output.

Regards,
Alister

Many thanks Alister! (and Vidyadhar85)