Exec command behaviour


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exec command behaviour
# 1  
Old 02-26-2013
Lightbulb Exec command behaviour

Hello All,

I came across this line in a shell script that I was looking at yesterday. Couldn't get a clue of what it does. Smilie

Code:
....
....
blah blah
....
.....

exec >&- 2>&-

......
blah blah
.....

Can someone explain me this??

Usually I have seen exec commands used to redirect std output and error output to log files. And this one is different! Smilie
# 2  
Old 02-26-2013
Looks like that program is closing stdout and stderr. C.f. man bash:
Quote:
Duplicating File Descriptors
The redirection operator
[n]<&word
is used to duplicate input file descriptors. If word expands to one or more digits, the file descriptor denoted by n is made to be
a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs.
If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
The operator
[n]>&word
is used similarly to duplicate output file descriptors . . .
# 3  
Old 02-26-2013
So RudiC,

I have this
Code:
exec >file1 2>&1
cmd1
cmd2
cmd3
exec >&- 2> &-
cmd4
cmd5

By your explanation, file1 will have the outputs of cmd1, cmd2 & cmd3.
Then exec >&- 2>&- will stop redirecting to file1. Thus outputs of cmd4 & cmd5 will be directed to stdout & stderr.

Is my understanding correct?

---------- Post updated at 03:12 PM ---------- Previous update was at 03:12 PM ----------

So RudiC,

I have this
Code:
exec >file1 2>&1
cmd1
cmd2
cmd3
exec >&- @> &-
cmd4
cmd5

By your explanation, file1 will have the outputs of cmd1, cmd2 & cmd3.
Then exec >&- 2>&- will stop redirecting to file1. Thus outputs of cmd4 & cmd5 will be directed to stdout & stderr.

Is my understanding correct?
# 4  
Old 02-26-2013
No, it closes stdout and stderr entirely - your shell won't have any output channel any more.
This User Gave Thanks to RudiC For This Post:
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. Red Hat

Ps command different behaviour

Hi Experts, ps command behavior in Redhat is such that it outputs all the output(of long lengths). In Unix the ps command output was limited to only 80 chars. In that if you pipe its output to another command hen the 80 chars restriction wouldn't be there. This 80 char limitation will only be... (14 Replies)
Discussion started by: Albert_Pinto7
14 Replies

3. Shell Programming and Scripting

Behaviour of pwd command in sh and ksh

I have a script as below. bash-3.00$ cat test.sh #!/usr/bin/ksh path=`pwd` echo $path var=$path/temp11 echo $var If run it is giving output bash-3.00$ ksh test.sh //var/tmp/SB2/miscellaneous //var/tmp/SB2/miscellaneous/temp11 (5 Replies)
Discussion started by: millan
5 Replies

4. 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

5. 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

6. 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

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 Advanced & Expert Users

Sort command - strange behaviour

Hi guys, I have the following example data: A;00:00:19 B;00:01:02 C;00:00:13 D;00:00:16 E;00:02:27 F;00:00:12 G;00:00:21 H;00:00:19 I;00:00:13 J;00:13:22 I run the following sort against it, yet the output is as follows: sort -t";" +1 -nr example_data.dat A;00:00:19 (16 Replies)
Discussion started by: miwinter
16 Replies

9. Shell Programming and Scripting

Help me to resolve uncertian behaviour of a sort command

I have got a file BeforeSort.txt having 40 fields seperated by "|" First field= RecordType (Value will be P or FP) Second field= CamCode Third field = UpdatingDate Fourth field = ProductType Fifth field = ActionCode (Value may be 01, 02 or 03) Sixth field = ProductCode and so on My... (1 Reply)
Discussion started by: pankajrai
1 Replies

10. 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
Login or Register to Ask a Question