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"

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

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

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.

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\)

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

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?

Ya. Have you go through the reference URL posted by Mr.Vivek raj?
It will clarify your queries

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:

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

Can i know the difference between [N]<&WORD and [N]>&WORD using graph

What do you mean?

they have mentioned in bash man page, Redirection section that:

for example 4<&1, 4>&1, 1>&4, 1<&4, 0>&4, 0<&4, 4>&0, 4<&0

how can we apply the above operators and what is the effect.

Any answer guys? if clarification is required please let me know

---------- Post updated at 10:08 AM ---------- Previous update was at 09:58 AM ----------

again i can say it in other words:

i need to know what is the difference between

for example, if we apply the below direction to the shell by executing them with exec, what is the effect:
4<&1
4>&1
1>&4
1<&4
0>&4
0<&4
4>&0
4<&0