home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / shell / 4635 < prev    next >
Encoding:
Text File  |  1992-11-10  |  2.6 KB  |  86 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!ukma!cs.widener.edu!eff!news.oc.com!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Bourne shell I/O
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Nov11.010515.21071@news.eng.convex.com>
  8. Date: Wed, 11 Nov 1992 01:05:15 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <1992Nov10.235519.3857@eng.ufl.edu>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 68
  17.  
  18. From the keyboard of ruck@zeta.ee.ufl.edu (John R Ruckstuhl Jr):
  19. :On several occasions, I've seen posters to comp.unix.shell claim that
  20. :there is no need to create temporary files in Bourne shell scripts --
  21. :that proper use of redirection and the available file descriptors 
  22. :(0-9 ?) are a better way to shuffle temporary data.
  23. :
  24. :I'm studying the man pages and I'm having trouble seeing how this works.
  25. :
  26. :Suppose I want to process stderr, then process stdout
  27. :The solutions I see must use a tempfile.
  28. :
  29. :e.g.:
  30. :
  31. :$ (echo one >&1; echo two >&2) 2>&1 > tempfile | sed 's/^/stderr /'
  32. :stderr two
  33. :$ sed 's/^/stdout /' tempfile
  34. :stdout one
  35. :$ rm tempfile
  36. :
  37. :Do any of you know how to use redirection cleverly to avoid creating 
  38. :(explicitly) tempfiles?
  39.  
  40. Yes.
  41.  
  42. :Or do you think I've misunderstood what the posters meant.
  43. :Heck, there must be a reason for the extra file descriptors, right?
  44.  
  45. Yes.
  46.  
  47. Consdier this:
  48.  
  49.     exec 3>&1; grep yyy xxx 2>&1 1>&3 3>&- | sed s/file/foobar/ 1>&2 3>&-
  50.  
  51. or this:
  52.  
  53.     device=/dev/rmt8
  54.     dd_noise='^[0-9]+\+[0-9]+ records (in|out)$'
  55.     exec 3>&1
  56.     status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) |
  57.                 egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1`
  58.     exit $status;
  59.  
  60.  
  61.  
  62. Those two examples were from the "Csh Considered Harmful" document.
  63.  
  64. This next one is from the Perl FAQ, but we're using traditional
  65. shell redirection, so you should be able to adapt it to straight
  66. shell.
  67.  
  68.     open (CMD, 
  69.       "3>&1 (cmd args 2>&1 1>&3 3>&- | sed 's/^/STDERR:/' 3>&-) 3>&- |");
  70.  
  71.     while (<CMD>) {
  72.       if (s/^STDERR://)  {
  73.       print "line from stderr: ", $_;
  74.       } else {
  75.       print "line from stdout: ", $_;
  76.       }
  77.     }
  78.  
  79.  
  80. --tom
  81. -- 
  82.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  83.  
  84.         "There is no reason for any individual to have a computer in their
  85.          home."  (Ken Olson, President, Digital Equipment, 1977)
  86.