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 ? |
Code:
cat xxx file 2>stderr 1>&2 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 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 |
Hi,
and welcome to LQ! Quote:
(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 |
Indeed, I just noticed a strange behaviour! If I do
Code:
$ cat file xxx 2>stderr 1>stderr Code:
$ cat xxx file 2>stderr 1>stderr |
To colucix
If you look at first example carefully.I redirect standard output and standard error to stderr like example2 Quote:
Quote:
in both example I redirect standard output and standard error to same file the "stderr" |
To colucix
I know what is result of this Code:
$ cat file xxx 2>stderr 1>stderr Code:
[root@localhost ~]# cat file xxx second I do Code:
[root@localhost ~]# cat xxx file I can explain your result Code:
$ cat file xxx 2>stderr 1>stderr Code:
[root@localhost ~]# cat file xxx Let try!!! So I confuse what make first example and second example different? |
Quote:
Quote:
In the first example when we do Code:
cat something 1>logfile 2>logfile |
To colucix
Thank you every much colucix. |
All times are GMT -5. The time now is 06:38 PM. |