howto:redirection_tutorial

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
howto:redirection_tutorial [2016/09/08 15:05]
anwar [exec] there should be only one echo
howto:redirection_tutorial [2019/02/23 04:49] (current)
razzed [Duplicating File Descriptor 2>&1]
Line 21: Line 21:
 # lsof +f g -ap $BASHPID -d 0,1,2 # lsof +f g -ap $BASHPID -d 0,1,2
 COMMAND ​  PID USER   ​FD ​  TYPE FILE-FLAG DEVICE SIZE/OFF NODE NAME COMMAND ​  PID USER   ​FD ​  TYPE FILE-FLAG DEVICE SIZE/OFF NODE NAME
-bash    12135 root    0u   ​CHR ​    RW,LG 136,​13 ​     0t0   16 /dev/pts/13 +bash    12135 root    0u   ​CHR ​    RW,LG 136,​13 ​     0t0   16 /dev/pts/5 
-bash    12135 root    1u   ​CHR ​    RW,LG 136,​13 ​     0t0   16 /dev/pts/13 +bash    12135 root    1u   ​CHR ​    RW,LG 136,​13 ​     0t0   16 /dev/pts/5 
-bash    12135 root    2u   ​CHR ​    RW,LG 136,​13 ​     0t0   16 /dev/pts/13+bash    12135 root    2u   ​CHR ​    RW,LG 136,​13 ​     0t0   16 /dev/pts/5
 </​code>​ </​code>​
  
Line 56: Line 56:
  
 the ''>​ file''​ after the command alters the file descriptors belonging to the the ''>​ file''​ after the command alters the file descriptors belonging to the
-command ​foo. It changes the file descriptor ''​1''​ (''>​ file''​ is the same as+command ​''​echo''​. It changes the file descriptor ''​1''​ (''>​ file''​ is the same as
 ''​1>​file''​) so that it points to the file ''​file''​. They will look like: ''​1>​file''​) so that it points to the file ''​file''​. They will look like:
  
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.1473347149.txt
  • Last modified: 2016/09/08 15:05
  • by anwar