File Descriptor redirection and duplication


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users File Descriptor redirection and duplication
# 1  
Old 03-02-2010
Question File Descriptor redirection and duplication

i have many questions concerning the FD.

it was stated that "to redirect Error to output std, you have to write the following code"

Code:
[root@testsrv3 ~]# ls -alt FileNotThere File > logfile 2>&1
[root@testsrv3 ~]# cat logfile
ls: cannot access FileNotThere: No such file or directory
-rw-r--r-- 1 root root 0 2010-02-26 05:34 File

but if i changed the order as follows, only the output will be stored in the file:
Code:
[root@testsrv3 ~]# ls -alt FileNotThere File 2>&1 > logfile
ls: cannot access FileNotThere: No such file or directory
[root@testsrv3 ~]# cat logfile
-rw-r--r-- 1 root root 0 2010-02-26 05:34 File

my question is, how we can use the logic to describe this?

maybe i have miss understand the redirection char '>' concept. as i understand, it forwards the contents of some fd to other fd. ie, 2>&1 will forward the stderror to stdout and if we add ">logfile" the contents of stdout (which include stdout and stderr) will be redirected to the file, but that is not happened


can anyone explain the fd duplication, and how we can create fd (other than 0,1 and 2) and how we can deal with them.


i know it is a long question but i need some guides because man pages and info bash (Redirection section) make me confused.
# 2  
Old 03-02-2010
generally the errors will be printed to the stderr.
In the first case at first you are redirecting the output to logfile file directly.
At this stage both the stdout and stderr are redirect to the logfile.
Then only redirecting the stderr to stdout.

(0 for stdin, 1 for stdout and 2 for stderr)

In the second case you are redirecting the stderr to the stdout.
So the error message is getting printed in the stderr and the stdout is redirected to the logfile.
So the output is stored in the logfile.

Last edited by thillai_selvan; 03-02-2010 at 04:18 AM..
# 3  
Old 03-02-2010
In your first case,

You're storing all the output to a logfile.After that,you're specifying 2>&1,which means the messages in stder have to be stored in stdout.Now,stdout is logfile.So,both output and error are stored.

In the second case,you're redirecting the stderr to stdout.Then,you're redirecting the stdout to a file.Because,you specified that whatever printed in stderr should be printed in stdout and whatever printing in stdout should be transformed to the file.That's why error printing in stdout and the output is redirected to logfile. For more informations,refer the following URL. http://en.wikipedia.org/wiki/Redirection_(computing)
# 4  
Old 03-02-2010
thanks for the reply, but if that is the case, how to know the flow of the command when there is multi redirection and piping?

from your answer, can i say that command stdout is redirected at the end of the command>? and stderr is redirected once it is received?

it is confusing thing, do u have a reference to that or guide? and what about creating and dealing with other FD?


From the second answer
Quote:
In the second case,you're redirecting the stderr to stdout.Then,you're redirecting the stdout to a file.Because,you specified that whatever printed in stderr should be printed in stdout and whatever printing in stdout should be transformed to the file.That's why error printing in stdout and the output is redirected to logfile
when i redirect stderr to stdout, is it like a pointer from fd 2 to fd1; in other words, whatever written to fd2 will be actually written to fd1. and once i write the content of fd1 (where the fd2 is also pointing) that will cause fd1 and fd2 contents to be written to file?

Last edited by ahmad.zuhd; 03-02-2010 at 04:27 AM..
# 5  
Old 03-02-2010
Ya. Have you go through the reference URL posted by Mr.Vivek raj?
It will clarify your queries
# 6  
Old 03-02-2010
i've checked the link, and it makes it better. but it doesn't include all the information. i'll continue searching the internet. However i have seen an example of creating a fd:

Code:
exec 5<&1
echo "TEST" >&5
exec 5>&-

as in the page, this was intended to redirect the stdout to the fd 5 and create it, and close it. i have the following questions:
- what is exactly the meaning of second command? is it to redirect the command stdout "test" to the fd 5? and how i can see the contents of the fd 5?
- in the first command, why the < is used instead if > and what is the difference between the below two commands as in the info bash *Redirection section
Quote:
3.6.7 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. If N is not
specified, the standard output (file descriptor 1) is used. If the
digits in WORD do not specify a file descriptor open for output, a
redirection error occurs. As a special case, if N is omitted, and WORD
does not expand to one or more digits, the standard output and standard
error are redirected as described previously.
# 7  
Old 03-03-2010
Can i know the difference between [N]<&WORD and [N]>&WORD using graph
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Log File Content - Duplication Issue

I have a shell script with 2 run time arguments. During the execution if i got any error, then it needs to redirected to a error file and in console. Also both error and output to be redirected to a log file. Error output is getting copied to err file and getting displayed in console too. But... (2 Replies)
Discussion started by: sarathy_a35
2 Replies

2. Shell Programming and Scripting

Variable File Descriptor

Greetings. I am happily using constructs like the following; I have set -x to show fine distinctions. $ exec 4> afile + exec + 4> afile $ print -u4 This is the first line in afileAs you can see from the -x expansion, the shell performed the exec command and redirected file descriptor to... (4 Replies)
Discussion started by: rpaskudniak
4 Replies

3. Shell Programming and Scripting

Help with File Descriptor in a While loop

Hi, I am trying to read a file line-by-line in a while loop, and perform some tasks which involves non-interactive SSH to a remote server. The code looks something like this -- #!/usr/bin/ksh export myFile=/path/to/my/file.load while read line do do something ## Adding the SSH... (2 Replies)
Discussion started by: Subu1987
2 Replies

4. Shell Programming and Scripting

Why stderr file descriptor redirection makes ksh's "select" construct hang.

I am trying to use one global declaration --> "exec 2>$ERR" to capture all stderr outputs that may occur anywhere in my script. Then close it at the end of the script using --> "exec 2<&-" I am using KSH on Solaris 8. KSH Version M-11/16/88i If I comment two "exec .." statements in the... (11 Replies)
Discussion started by: kchinnam
11 Replies

5. Shell Programming and Scripting

File Duplication Script?

I have a file, let's say 1.jpg, and I have a text file that contains a list of filenames I would like to duplicate 1.jpg as (i.e., 2.jpg, 3.jpg, 4.jpg, etc.). The filenames that I want to create are all on separate lines, one per line. I'm sure there's a simple solution, but I'm not claiming to... (7 Replies)
Discussion started by: futurestar
7 Replies

6. UNIX for Dummies Questions & Answers

File Descriptor

Hi What the below path contains? /proc/<pid>/fd (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

7. Shell Programming and Scripting

File Descriptor

Hello All, Im opening a file desciptor in perl and sending data using print CMD "$xyz". is there a limit to the length of the string that I can give to this CMD at a time. (3 Replies)
Discussion started by: rimser9
3 Replies

8. UNIX for Dummies Questions & Answers

File Descriptor Help

What is a file descriptor in Unix?? How to find a file descriptor of a file in Unix?? Does it have anything to do with the Inode numbers?? (3 Replies)
Discussion started by: rahulrathod
3 Replies

9. Windows & DOS: Issues & Discussions

File Duplication

hi all how to find the file duplication in a windows 2000 server as usual replies are sincerely appreciated. thanks raguram R (3 Replies)
Discussion started by: raguramtgr
3 Replies

10. UNIX for Dummies Questions & Answers

file activity (open/closed) file descriptor info using KORN shell scripting

I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies
Login or Register to Ask a Question