home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / shell / 3951 < prev    next >
Encoding:
Text File  |  1992-09-14  |  1.8 KB  |  46 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: Re: clever `cat' commmand?
  5. Message-ID: <1992Sep15.022614.12085@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <1992Sep14.173218*Harald.Eikrem@delab.sintef.no> <lb9kc0INN78u@news.bbn.com>
  8. Date: Tue, 15 Sep 92 02:26:14 GMT
  9. Lines: 35
  10.  
  11. > For once, a question.  Can anyone think of the simplest means of making
  12. > a utility that behaves like `cat(1)', but exits with 0 if something went
  13. > through it, otherwise 1?  Preferrably a one-liner......
  14. > I've tried [an awk solution]
  15. > which is ok with a line oriented input stream, but not with a "binary" 
  16. > stream.
  17.  
  18. Many of the utilities commonly used to build shell scripts don't work
  19. reliably with "binary" streams, so this isn't surprising.  The proposed
  20. solution "grep ." suffers from this problem, and in addition the pattern
  21. "." is wrong; it should be, say, "^" to match even empty lines.
  22.  
  23. One utility that does work on binary streams is dd.  The version I'm familiar
  24. with has the additional property that it verbosely outputs count information
  25. on stderr.  On the system I'm typing this on, the following sh one-liner
  26. does exactly what is wanted:
  27.  
  28.     (dd 2>&1 >&3 | grep -s -v '^0+0 ') 3>&1
  29.  
  30. The '^0+0 ' matches both lines of dd's stderr when nothing was passed through,
  31. and neither line otherwise.
  32.  
  33. dd doesn't take filename arguments in the usual way, so prefix this with
  34.  
  35.     cat "$@" |
  36.  
  37. if you want arguments to be interpreted as in cat.  Sufficiently old sh's
  38. may require something more verbose than "$@" to correctly handle the case
  39. of zero arguments.
  40. -- 
  41. Mark Brader            "You have a truly warped mind.
  42. SoftQuad Inc., Toronto             I admire that in a person."
  43. utzoo!sq!msb, msb@sq.com                -- Bill Davidsen
  44.  
  45. This article is in the public domain.
  46.