home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / shell / 3946 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  1.6 KB

  1. Path: sparky!uunet!olivea!news.bbn.com!news.bbn.com!news
  2. From: cjross@bbn.com (chris ross)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: clever `cat' commmand?
  5. Message-ID: <lb9kc0INN78u@news.bbn.com>
  6. Date: 14 Sep 92 17:53:04 GMT
  7. References: <1992Sep14.173218*Harald.Eikrem@delab.sintef.no>
  8. Lines: 42
  9. NNTP-Posting-Host: bbn.com
  10.  
  11. Harald.Eikrem@delab.sintef.no writes:
  12.  
  13. >For once, a question.  Can anyone think of the simplest means of making
  14. >a utility that behaves like `cat(1)', but exits with 0 if something went
  15. >through it, otherwise 1?  Preferrably a one-liner......
  16.  
  17. >I've tried:   awk '{ s=1; print; }; END{ if(s) exit 0; exit 1; }'
  18.  
  19. >which is ok with a line oriented input stream, but not with a "binary" 
  20. >stream.
  21.  
  22. Your grep may be able to accomplish this.
  23. Use grep -s if you don't want the output.
  24.  
  25.   $ echo foo | grep .
  26.   foo
  27.   $ echo $?
  28.   0
  29.   $ cat </dev/null | grep .
  30.   $ echo $?
  31.   1
  32.  
  33. If not, try this in place of the grep command, with
  34. appropriate mods if you're using csh.  Use "cat >" instead
  35. of "tee" if you don't want the output.
  36.  
  37.   (cd /tmp; tee $$; test -s $$; S=$?; rm $$; exit $S)
  38.  
  39. Or, if you're paranoid about cleaning up if signalled:
  40.  
  41.   (cd /tmp; tee $$; trap 'rm $$; exit $S' 0 1 2 3 13 15; test -s $$; S=$?)
  42.  
  43. (You _did_ ask for a one-liner :-)
  44. Here are perl solutions, with and without output:
  45.  
  46.   perl -e '$x = 1, print while <STDIN>; exit !$x'
  47.   perl -e 'exit !<STDIN>'
  48.  
  49.  
  50. chris ross   <cjross@bbn.com>   BBN Advanced Simulations   (617) 873-3272
  51.  WARNING: article may contain flammable material.  Do not expose to open
  52.    flames.  In case of accidental ignition, douse keyboard with water.
  53.