home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!news.bbn.com!news.bbn.com!news
- From: cjross@bbn.com (chris ross)
- Newsgroups: comp.unix.shell
- Subject: Re: clever `cat' commmand?
- Message-ID: <lb9kc0INN78u@news.bbn.com>
- Date: 14 Sep 92 17:53:04 GMT
- References: <1992Sep14.173218*Harald.Eikrem@delab.sintef.no>
- Lines: 42
- NNTP-Posting-Host: bbn.com
-
- Harald.Eikrem@delab.sintef.no writes:
-
- >For once, a question. Can anyone think of the simplest means of making
- >a utility that behaves like `cat(1)', but exits with 0 if something went
- >through it, otherwise 1? Preferrably a one-liner......
-
- >I've tried: awk '{ s=1; print; }; END{ if(s) exit 0; exit 1; }'
-
- >which is ok with a line oriented input stream, but not with a "binary"
- >stream.
-
- Your grep may be able to accomplish this.
- Use grep -s if you don't want the output.
-
- $ echo foo | grep .
- foo
- $ echo $?
- 0
- $ cat </dev/null | grep .
- $ echo $?
- 1
-
- If not, try this in place of the grep command, with
- appropriate mods if you're using csh. Use "cat >" instead
- of "tee" if you don't want the output.
-
- (cd /tmp; tee $$; test -s $$; S=$?; rm $$; exit $S)
-
- Or, if you're paranoid about cleaning up if signalled:
-
- (cd /tmp; tee $$; trap 'rm $$; exit $S' 0 1 2 3 13 15; test -s $$; S=$?)
-
- (You _did_ ask for a one-liner :-)
- Here are perl solutions, with and without output:
-
- perl -e '$x = 1, print while <STDIN>; exit !$x'
- perl -e 'exit !<STDIN>'
-
-
- chris ross <cjross@bbn.com> BBN Advanced Simulations (617) 873-3272
- WARNING: article may contain flammable material. Do not expose to open
- flames. In case of accidental ignition, douse keyboard with water.
-