Exec command - what is it doing here?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exec command - what is it doing here?
# 1  
Old 05-13-2013
Code 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:

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
# 2  
Old 05-13-2013
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 Smilie
# 3  
Old 05-13-2013
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
# 4  
Old 05-13-2013
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:
Code:
exec 1>nul 2>nul

?

Thanks for the Cygwin tip!
# 5  
Old 05-13-2013
Quote:
Originally Posted by el_foz
If I wanted to cancel this behaviour later in the script, how would I do that?
Code:
exec 1>nul 2>nul

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
# 6  
Old 05-13-2013
Many thanks Alister! (and Vidyadhar85)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

3. Shell Programming and Scripting

exec command help

Hi, I have the following lines in a script : . . exec < some_file . . . I have very little idea about exec command. I would like to know what this does and what will happen if the file some_file does not exist. Specifically, I would like to know whether the lines following this... (5 Replies)
Discussion started by: elixir_sinari
5 Replies

4. UNIX for Dummies Questions & Answers

Exec command

Hi can some one explain the following command , It would really help if some can really elloborate on what is happening out here export PATH | exec /bin/sh ./auto_approve :q P.S: This is the first time i am using exec ,so an elloboration what does it do and what is the use of the :q will be... (1 Reply)
Discussion started by: Sri3001
1 Replies

5. Shell Programming and Scripting

exec command

How can I use the exec command to log my korn shell session to the screen and the log file? Currently I have this command: $exec 1> ${LOG} 2>&1 This logs the output to the log file only. I want it to go to the screen also. Is this possible with this command? thanks. (10 Replies)
Discussion started by: djehresmann
10 Replies

6. Shell Programming and Scripting

exec command help

All, I am using below shell script to output the content to outputfile.txt. What I am looking for is in addition to outputfile.txt, I want the output to be on standard output too. exec > outputfile.txt echo "Starting " echo "ending" (5 Replies)
Discussion started by: basic_shell
5 Replies

7. Shell Programming and Scripting

exec command

can any one pls explain the meaning of exec 1<&5 ?? its urgent (2 Replies)
Discussion started by: santosh1234
2 Replies

8. UNIX for Dummies Questions & Answers

Need help with -exec cp command.

I have a ksh script that contains the following: find /dir1/dir2 -type f -name "FILE.*" -newer /dir1/dir2/afterme.txt -exec cp /dir1/dir2/dir3 {} \; When I run it from the cli, it runs fine. When I run it from the ksh script I get find: missing argument to `-exec' I also tried -exec cp... (40 Replies)
Discussion started by: bbbngowc
40 Replies

9. Shell Programming and Scripting

exec command

hai i want know the difference between two shell scripts those are 1) a=2004 echo $a #output------2004 exec < inputfile while read line do echo $a #output-------2004 a=2005 echo $line echo $a ... (1 Reply)
Discussion started by: g_s_r_c
1 Replies

10. UNIX for Dummies Questions & Answers

using the -exec command

linux redhat 8.0 I am getting accustomed to using the -exec command to get around my databse.. and use it to edit and update files..! is this more apllicable than jumping from one directory to the other.. I have set up the databse so that the inode #'s are accessable and can get me from one... (0 Replies)
Discussion started by: moxxx68
0 Replies
Login or Register to Ask a Question