Jump to content

File Descriptor redirection and duplication

ahmad.zuhd's Photo
Posted Mar 02 2010 11:26 PM
4337 Views

i have many questions concerning the FD. I have Two parts question:


Part I:
it was stated that "to redirect Error to output std, you have to write the following 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:


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


Part II:
i have seen an example of creating a fd:
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.



i know it is a long question but i need some guides because man pages and info bash (Redirection section) make me confused.

Tags:
1 Subscribe


1 Reply

0
  KBenson's Photo
Posted Mar 17 2010 03:23 PM

Part I

It may help to think of the file descriptors as variables that contain a value, and the value determines where the output goes (STDOUT, STDERR, a file, etc).

When redirecting 2 (STDERR) to 1 (STDOUT), the following happens for the the two cases you described:

1) Redirect STDERR before STDOUT
[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

Here we are changing FD 2 to point to the same target FD 1 CURRENTLY points at, STDOUT. Then we change FD 1 to redirect to a file, logfile. This leaves FD 2 still containing the target location of STDOUT, since it now contains the value of FD 1 at the time it was redirected.

2) Redirect STDERR after STDOUT
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

Here we are redirecting FD 1 to a file, logfile, and then redirecting FD 2 to the same location as FD 1 is pointing, which is currently logfile. Thus, both the default file desciptors for STDIN and STDOUT sending inputs to the file.

So, as you can see, it can be useful to think of file descriptors as variables holding a value, and by redirecting them you aren't chaining them through another FD, but reassigning their current value to the same value as the target FD.

Indeed, reading the bash man page shows they refer to the 2>&1 syntax as a file descriptor duplication, not redirection, which is the case when used without & (redirect inputs to a file).

Part II

I have to admit I'm a bit unsure about how this is functioning, but I assume that FD 5 is opened for input, and the default action for output is to display on STDOUT, which is why you can echo a string to FD 5 afterwards and see it on STDOUT. That's a complete guess, but it makes sense to me that the input and output portions of a FD would take STDIN and STDOUT as their default values.