home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume1 / 8711 / 8 < prev    next >
Internet Message Format  |  1990-07-13  |  4KB

  1. Path: uunet!husc6!rutgers!clyde!cbosgd!mandrill!hal!ncoast!allbery
  2. From: tcjones@watdragon.waterloo.edu (Crocodile Dundee)
  3. Newsgroups: comp.sources.misc
  4. Subject: pipe - multi-in multi-out pipelining BUG and fix.
  5. Message-ID: <5641@ncoast.UUCP>
  6. Date: 14 Nov 87 20:32:03 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Organization: U. of Waterloo, Ontario
  9. Lines: 145
  10. Approved: allbery@ncoast.UUCP
  11. X-Archive: comp.sources.misc/8711/8
  12.  
  13. Ooops. It turns out that under certain conditions "pipe" will do strange
  14. things. This occurred when someone did the following
  15.  
  16.     cat fred | pipe -o ">list1" "|sort>list2"
  17.  
  18. The script was copying its input to its output with
  19.  
  20.     while read line
  21.     do
  22.         echo $line >> wherever
  23.     done
  24.  
  25. and this got totally mixed up when the input line contained a "*"
  26. which was passed on to echo and expanded into all the filenames in the
  27. directory. I should have used cat in the first place and all this wouldn't have
  28. gone on. Anyway, here it is again, fixed (and faster due to cat).
  29.  
  30. Terry
  31.  
  32. # This is a shell archive.  Remove anything before this line,
  33. # then unpack it by saving it in a file and typing "sh file".
  34. #
  35. # Wrapped by watdragon!tcjones on Tue Nov 10 18:38:38 EST 1987
  36. # Contents:  pipe
  37.  
  38. echo x - pipe
  39. sed 's/^@//' > "pipe" <<'@//E*O*F pipe//'
  40. #!/bin/sh
  41.  
  42. #
  43. # pipe  --  multi-input, multi-output pipelining
  44. #
  45. #           Terry Jones 19/10/87 (tcjones@watdragon)
  46. #
  47. #-------------------------------------------------------------------------------
  48. #             Department Of Computer Science, University Of Waterloo
  49. #             Waterloo Ontario Canada N2L 3G1
  50. #
  51. #{ihnp4,allegra,decvax,utzoo,utcsri,clyde}!watmath!watdragon!tcjones
  52. #tcjones@dragon.waterloo.{cdn,edu} tcjones@WATER.bitnet
  53. #tcjones%watdragon@waterloo.csnet [from oz, tcjones@dragon.waterloo.cdn@munnari]
  54. #-------------------------------------------------------------------------------
  55. #
  56.  
  57. myname=`basename $0`
  58.  
  59. if [ $# -eq 0 ]
  60. then
  61.     cat > /dev/tty
  62.     exit 1
  63. fi
  64.  
  65. def_shell=/bin/sh
  66.  
  67. if [ -z "$SHELL" ]
  68. then
  69.     echo No '$SHELL' variable set, using $def_shell
  70.     SHELL=$def_shell
  71. fi
  72.  
  73. cum_in=/tmp/pipe_$$
  74.  
  75. if [ -f $cum_in -a ! -w $cum_in ]
  76. then
  77.     echo ${myname}: could not use temporary ${cum_in} - try again.
  78.     exit 1
  79. fi
  80.  
  81. >$cum_in
  82. tty=`tty`
  83.  
  84. IN=1
  85. SOME_IN=0
  86. SOME_OUT=0
  87. ANY_OUT=0
  88. while [ -n "$1" ]
  89. do
  90.     case $1 in
  91.         -in|-i) 
  92.             IN=1
  93.             SOME_IN=0
  94.  
  95.             if [ "$SOME_OUT" = "0" ]
  96.             then
  97.                 SOME_OUT=1
  98.                 cat $cum_in
  99.             fi
  100.  
  101.             shift;;
  102.         -out|-o)
  103.             IN=0
  104.             SOME_OUT=0
  105.             ANY_OUT=1
  106.  
  107.             if [ "$SOME_IN" = "0" ]
  108.             then
  109.                 SOME_IN=1
  110.                 cat >> $cum_in
  111.             fi
  112.             shift;;
  113.         *) 
  114.             if [ "$IN" = "1" ]
  115.             then
  116.                 SOME_IN=1
  117.                 if [ "$1" = "-" ]
  118.                 then
  119.                     cat >> $cum_in
  120.                 else
  121.                     echo $1 | $SHELL >> $cum_in
  122.                 fi
  123.             else
  124.                 SOME_OUT=1
  125.                 if [ "$1" = "-" ]
  126.                 then
  127.                     cat $cum_in
  128.                 else
  129.                     eval "cat $cum_in" "$1"
  130.                 fi
  131.             fi
  132.  
  133.             shift;;
  134.     esac
  135. done
  136.  
  137. if [ "$SOME_OUT" = "0" -o "$ANY_OUT" = "0" ]
  138. then
  139.     cat $cum_in
  140. fi
  141.  
  142. /bin/rm -f $cum_in
  143. @//E*O*F pipe//
  144. chmod u=rwx,g=,o= pipe
  145.  
  146. echo Inspecting for damage in transit...
  147. temp=/tmp/shar$$; dtemp=/tmp/.shar$$
  148. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  149. cat > $temp <<\!!!
  150.      103     207    2054 pipe
  151. !!!
  152. wc  pipe | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  153. if [ -s $dtemp ]
  154. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  155. else echo "No problems found."
  156. fi
  157. exit 0
  158.