LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I/O redirection (https://www.linuxquestions.org/questions/linux-newbie-8/i-o-redirection-578633/)

jittinan2 08-21-2007 03:58 AM

I/O redirection
 
Hi Everybody I am linux newby.If anybody knows please answer my question.

I create one file in my linux system.its name is "file" contain the string "filecontent".

To test redirection in linux I do following.

[root@localhost ~]# cat file
filecontent
[root@localhost ~]# cat xxx file 2>stderr 1>stderr
[root@localhost ~]# cat stderr
filecontent
such file or directory
[root@localhost ~]#

I think when "cat" command read the first parameter "xxx" because xxx does not exits so it send error to file "stderr" after that cat command consume second parameter "file" becuse this file is exits then cat command send "filecontent" string to stderr same as error message but output string "filecontent" overwrite the existing message "cat: xxx: No such file or directory" as shown above.It's look like filedescriptor 1 and 2 have it own file pointer point to stderr separately.Does I conclude correctly?

I test another example.

[root@localhost ~]# cat xxx file 2>stderr 1>&2
[root@localhost ~]# cat stderr
cat: xxx: No such file or directory
filecontent
[root@localhost ~]#

I redirect standard error to stdrr and standard output to stderr but output result it look like cat command append "filecontent" after the error It's different from first example which output overwrite error message.

Who can explain me ?

colucix 08-21-2007 05:01 AM

Code:

cat xxx file 2>stderr 1>&2
First, take in mind that order is important! In the command above you tell to redirect standard error (file descriptor 2) to a file and both standard error and standard output to the same location. In this way, your guess is right: the standard error and the standard output do not overwrite each other if they are redirected to the same file! Otherwise it would have any sense.
The same applies when you do not redirect to a file at all: in this case both standard error and standard output are shown in the terminal.
Now, you can try what happens if you change the order of redirection in the above command:
Code:

cat xxx file 1>&2 2>stderr
Have fun! ;)

Edit: another note: if you want the same behaviour from your first example, you have to explicitly append the standard error and standard output to the same file, as in:
Code:

cat xxx file 2>>stderr 1>>stderr

Tinkster 08-21-2007 05:04 AM

Hi,

and welcome to LQ!
Quote:

Originally Posted by jittinan2 (Post 2865538)
I think when "cat" command read the first parameter "xxx" because xxx does not exits so it send error to file "stderr" after that cat command consume second parameter "file" becuse this file is exits then cat command send "filecontent" string to stderr same as error message but output string "filecontent" overwrite the existing message "cat: xxx: No such file or directory" as shown above.It's look like filedescriptor 1 and 2 have it own file pointer point to stderr separately.Does I conclude correctly?

I think that the result you see is a timing issue because they
(both STDERR and STDOUT) *are* writing to the same file at the
same time; I wouldn't put my hand in the fire for this, though.


Cheers
Tink

colucix 08-21-2007 05:21 AM

Indeed, I just noticed a strange behaviour! If I do
Code:

$ cat file xxx 2>stderr 1>stderr
$ cat stderr
cat: xxx: No such file or directory

as expected, but if I change the order of the arguments (input files)
Code:

$ cat xxx file 2>stderr 1>stderr
$ cat stderr
filecontent
 such file or directory

I agree with Tinkster: it can be a timing issue!

jittinan2 08-21-2007 05:30 AM

To colucix

If you look at first example carefully.I redirect standard output and standard error to stderr like example2

Quote:

[root@localhost ~]# cat file
filecontent
[root@localhost ~]# cat xxx file 2>stderr 1>stderr
[root@localhost ~]# cat stderr
filecontent
such file or directory
[root@localhost ~]#
but "filecontent" string overwrite on error message

Quote:

[root@localhost ~]# cat stderr
filecontent
such file or directory
in example2 output can not overwrite on error message this point make confuse why ? Because
in both example I redirect standard output and standard error to same file the "stderr"

jittinan2 08-21-2007 06:09 AM

To colucix

I know what is result of this

Code:

$ cat file xxx 2>stderr 1>stderr
$ cat stderr
cat: xxx: No such file or directory

first I do

Code:

[root@localhost ~]# cat file xxx
filecontent
cat: xxx: No such file or directory
[root@localhost ~]#


second I do

Code:

[root@localhost ~]# cat xxx file
cat: xxx: No such file or directory
filecontent
[root@localhost ~]#

you will see if I change order of parameter result also change.If I put xxx to be first parameter the error message will be shown before output.but if I put xxx to be second parameter error message will be shown after "filecontent".It result the same everytime so I am sure It is not timing issue.but I think cat command execute parameter from left to right

I can explain your result

Code:

$ cat file xxx 2>stderr 1>stderr
$ cat stderr
cat: xxx: No such file or directory

"filecontent" is written to terminal first from offset 0,after "cat: xxx: No such file or directory" is also written to offset 0 too.Because second string is longer than "filecontent" so "filecontent" is completely overwrite by second error message.if you change content in a "file".Make it longer than error message you will see some string from output some string from error message

Code:

[root@localhost ~]# cat file xxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
cat: xxx: No such file or directory
[root@localhost ~]# cat file xxx 1>stderr 2>stderr
[root@localhost ~]# cat stderr
cat: xxx: No such file or directory
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[root@localhost ~]#

You see "xxx....xx" on second line because when error message was written on output It also append '\n' .Actually it is "cat: xxx: No such file or directory\n".

Let try!!!

So I confuse what make first example and second example different?

colucix 08-21-2007 08:34 AM

Quote:

Because second string is longer than "filecontent" so "filecontent" is completely overwrite by second error message.if you change content in a "file".Make it longer than error message you will see some string from output some string from error message
Yes, good! Furthermore it is a timing issue in the sense that the file opened by the first file descriptor is not closed before the second file descriptor write in the same file. Indeed, "overwriting" should be an "empty + write" process!

Quote:

So I confuse what make first example and second example different?
The expression 2>&1 litteraly means "standard error is sent to the same place as standard output" so there is only one file descriptor (the standard output) which actually is redirected to a file. In other words, the standard error is sent to standard output and then the standard output is written.

In the first example when we do
Code:

cat something 1>logfile 2>logfile
two different file descriptors are attempting to redirect to the same file (almost) simultaneously. With the timing issue above, which you helped to clarify.

jittinan2 08-21-2007 08:01 PM

To colucix

Thank you every much colucix.


All times are GMT -5. The time now is 06:38 PM.