LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-21-2007, 04:58 AM   #1
jittinan2
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Rep: Reputation: 0
Smile 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 ?
 
Old 08-21-2007, 06:01 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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

Last edited by colucix; 08-21-2007 at 06:08 AM.
 
Old 08-21-2007, 06:04 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi,

and welcome to LQ!
Quote:
Originally Posted by jittinan2 View Post
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
 
Old 08-21-2007, 06:21 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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!
 
Old 08-21-2007, 06:30 AM   #5
jittinan2
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Original Poster
Rep: Reputation: 0
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"
 
Old 08-21-2007, 07:09 AM   #6
jittinan2
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Original Poster
Rep: Reputation: 0
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?

Last edited by jittinan2; 08-21-2007 at 07:12 AM.
 
Old 08-21-2007, 09:34 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 08-21-2007, 09:01 PM   #8
jittinan2
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Original Poster
Rep: Reputation: 0
To colucix

Thank you every much colucix.
 
  


Reply

Tags
superman


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Squid Redirection ajkannan83 Linux - Networking 2 02-15-2007 04:16 AM
redirection bhuvana Linux - General 2 07-03-2006 03:36 AM
Transparent redirection Hugo Orlando Linux - Networking 4 05-12-2005 06:26 PM
error redirection raumohir Linux - Newbie 4 03-30-2005 01:13 PM
redirection in C pantera Programming 2 08-11-2004 02:06 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration