Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
howto:redirection_tutorial [2019/02/23 04:01]
razzed
howto:redirection_tutorial [2019/02/23 04:49] (current)
razzed [Duplicating File Descriptor 2>&1]
Line 190: Line 190:
 and standard output to the file. Continue reading for more on this. and standard output to the file. Continue reading for more on this.
  
-So if you have file descriptor ​like:+So if you have two file descriptors ''​s''​ and ''​t'' ​like:
 <​code>​ <​code>​
                   ---       ​+-----------------------+                   ---       ​+-----------------------+
- a descriptor ​   ( ) ---->| /​some/​file ​           |+ a descriptor ​   ( ) ---->| /​some/​file ​           ​
 +                  ---       ​+-----------------------+ 
 +                  ---       ​+-----------------------+ 
 + a descriptor ​   ( t ) ---->| /​another/​file ​        |
                   ---       ​+-----------------------+                   ---       ​+-----------------------+
 </​code>​ </​code>​
-Using a ''​m>&n''​ (where ''​m'' ​is a number) you got a copy of this descriptor:+ 
 +Using a ''​t>&s''​ (where ''​t'' ​and ''​s''​ are numbersit means:  
 + 
 +> Copy whatever file descriptor ''​s''​ contains into file descriptor ''​t''​ 
 + 
 +So you got a copy of this descriptor: 
 <​code>​ <​code>​
                   ---       ​+-----------------------+                   ---       ​+-----------------------+
- a descriptor ​   ( ) ---->| /​some/​file ​           |+ a descriptor ​   ( s ) ---->| /​some/​file ​           | 
 +                  ---       ​+-----------------------+ 
 +                  ---       ​+-----------------------+ 
 + a descriptor ​   ( t ) ---->| /​some/​file ​           |
                   ---       ​+-----------------------+                   ---       ​+-----------------------+
 </​code>​ </​code>​
  
-Note that the positions are also duplicated. If you have already +Internally each of these is represented by a file descriptor opened by the operating system'​s ''​fopen''​ calls, and is likely just a pointer to the file which has been opened for reading (''​stdin''​ or file descriptor ''​0''​) or writing (''​stdout''​ /''​stderr''​).  
-read a line of ''​n'',​ then after ''​n>&m''​ if you read a line from ''​m'',​ you will + 
-get the second line of the file.+Note that the file reading or writing ​positions are also duplicated. If you have already 
 +read a line of ''​s'',​ then after ''​t>&s''​ if you read a line from ''​t'',​ you will 
 +get the second line of the file.  
 + 
 +Similarly for output file descriptors,​ writing a line to file descriptor ''​s''​ will append a line to a file as will writing a line to file descriptor ''​t''​. 
 + 
 +<note tip>The syntax is somewhat confusing in that you would think that the arrow would point in the direction of the copy, but it's reversed. So it's ''​target>&​source''​ effectively.</​note>​ 
 + 
 +So, as a simple example (albeit slightly contrived), is the following:​ 
 + 
 +<​code>​ 
 +exec 3>&​1 ​        # Copy 1 into 3 
 +exec 1> logfile ​  # Make 1 opened to write to logfile 
 +lotsa_stdout ​     # Outputs to fd 1, which writes to logfile 
 +exec 1>&​3 ​        # Copy 3 back into 1 
 +echo Done         # Output to original stdout 
 +</​code>​
  
  
  • howto/redirection_tutorial.txt
  • Last modified: 2019/02/23 04:49
  • by razzed