home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume1 / 8711 / 5 / pipe_doc < prev   
Text File  |  1990-07-13  |  5KB  |  142 lines

  1.  
  2.  
  3. Here is a thing for those times when the shell input/output facilities
  4. just wont let you do what you want to in any simple manner. Basically
  5. it's a multi-input multi-output pipe command.
  6.  
  7. Call it "pipe".
  8.  
  9. The idea is that the input is sequentially assembled from possibly several 
  10. sources and written (at possibly different times during the assembly) to 
  11. possibly several destinations. 
  12.  
  13. The input sources can be the terminal or the output of any other command -
  14. even the output of another "pipe".
  15.  
  16. The output can be to the terminal, or to any normal command - including a 
  17. normal pipeline, or to the input of another "pipe".
  18.  
  19. The syntax is easy. Command line arguments are one of three things, 
  20.  
  21.         1) the word "-in"
  22.         2) the word "-out"
  23.         3) a command
  24.     
  25. -in switches you to "pipe input mode", -out switches you to "pipe output mode".
  26. A pipe always begins in input mode. An example might make this clear...
  27.     
  28.         % pipe -in "echo hello" -out "|wc"
  29.         1       1       6
  30.  
  31. Which is to say, "Go into input mode, take input from the 
  32. command 'echo hello' (which of course produces the single word 'hello'), 
  33. switch to output mode, and send the output so far into the command '|wc'"
  34.  
  35. Notice that the commands above ("echo hello" and "|wc") must be passed to 
  36. pipe as a single argument. Thus it is necessary to enclose multi-word commands 
  37. in quotes. Also it is necessary to escape characters such as "|" and ">" from 
  38. the shell. "" quotes do both of these jobs nicely, while still allowing for the
  39. expansion of environment variable and commands in backquotes.
  40.  
  41. "-in" and "-out" can be abbreviated to "-i" and "-o" respectively and so 
  42. this could have been written as 
  43. pipe -i "echo hello" -o "|wc"
  44.  
  45. or, (since we start off in input mode) as
  46. pipe "echo hello" -o "|wc"
  47.  
  48. Now this could have been done easily with "echo hello | wc" using a normal
  49. one-in, one-out pipeline. More sophisticated examples will be coming up.
  50.  
  51. To take input from standard input, or write to standard output, use a "-"
  52. So,
  53.         % pipe - -o -
  54.         hello there
  55.         ^D              # have to close standard input with EOF
  56.         hello there
  57.         %
  58.  
  59. does the same thing as "cat" (alone on a line) would have done, 
  60. reading from the terminal and writing back to the terminal.
  61.  
  62. If you immediately go into output mode, pipe will assume you want to provide
  63. initial input from standard input, so the above could be written as
  64. "pipe -o -". If you don't give an output directive, pipe will assume you want
  65. standard output, so the last example could have been written simply as "pipe".
  66.  
  67. That's about all there is to know, here are some examples
  68.  
  69.         % pipe - "grep tcjones /etc/passwd" - -o "|mail tcjones" -
  70.         Here is my entry in /etc/passwd
  71.         ^D
  72.         Hope it's what you want.
  73.         Terry.
  74.         ^D
  75.         %
  76.         Here is my entry in /etc/passwd
  77.         tcjones:Wwo5wq.gu/wd.:134:113:Crocodile Dundee,MC6129x6687,8843852,88463
  78.         38,103 minota hagey residence:/u2/tcjones:/usr/public/itcsh
  79.         Hope it's what you want.
  80.         Terry.
  81.         %
  82.  
  83. Which takes a line from my terminal, concatenates the output of grep, 
  84. concatenates another line from my terminal, sends it all off in mail to me, and
  85. puts the output on the screen as well. Decidably more useful than the earlier 
  86. examples.
  87.  
  88. Here's another,
  89.  
  90.         % pipe - "cat last_lines" -o "|sort >sorted" ">unsorted" "|wc -l"
  91.         here is a sample line
  92.         here is another
  93.         and a third
  94.         ^D
  95.                32
  96.         %
  97.  
  98. which takes lines from my terminal, concatenates the contents of the file
  99. last_lines, sends all that into sort (which sorts the lot and leaves the output
  100. in the file "sorted"), puts a copy in the file "unsorted" and also sends the 
  101. lot to "wc -l" so I can see how many lines there were.
  102.  
  103. (The last_lines file already contained 29 lines)
  104.  
  105. It is always possible to omit the "-" command from a "-o" or "-i" section,
  106. provided that there is no other command in this section - which is to say that
  107. the section must be left empty.
  108.  
  109. It is possible to switch back and forth between input and output modes,
  110.  
  111.         % pipe -o "|wc" -i -o "|wc" -i -o "|wc"
  112.         this
  113.         ^D
  114.                1       1       5
  115.         is
  116.         ^D
  117.                2       2       8
  118.         the first
  119.         ^D
  120.                3       4      18
  121.         %
  122.  
  123. which takes lines from the terminal until an EOF, sends them to "wc",
  124. switches back to input mode (note there are no commands and so standard input
  125. will be used) to read more lines from the terminal, sends the current input to
  126. "wc" and so on and on... To have the whole thing sent to the screen at the end
  127. I could have appended a "-" OR used another "-o" with no command.
  128.  
  129. Notice that the output of wc in each case reflects the cumulative way in which
  130. the input is being assembled.
  131.  
  132. As a final example, you can intermix pipes with unix pipelines and i/o
  133. redirection to create complicated things like
  134.  
  135.         % pipe "echo this" | pipe - "pipe 'echo is'" | pipe - "echo dumb"
  136.         this
  137.         is
  138.         dumb
  139.         %
  140.  
  141. Which could be used to do more useful things. That's probably enough...
  142.