[OT] writing filters in sh

Chip Camden sterling at camdensoftware.com
Thu Oct 28 16:17:17 UTC 2010


Quoth Chad Perrin on Thursday, 28 October 2010:
> On Thu, Oct 28, 2010 at 08:11:48AM -0700, Chip Camden wrote:
> > 
> > Here's a way to do what you're wanting to do.  Unfortunately, it isn't a
> > generalized, single construct:
> > 
> > #!/bin/sh
> > if [ $# -ge 1 ];then
> >   exec cat $@ | $0
> >   exit
> > fi
> > 
> > while read data; do
> >   echo $data
> > done
> > 
> > My lame attempts to generalize the first paragraph into an alias,
> > function, or shell script have met with disappointment.
> 
> I was hoping for a generalized, simple idiom for this, rather than
> needing to implement it myself, for demonstration purposes (and for easy
> reuse later, of course).  Your solution does not exactly fit my
> preferences for simplicity, but I might include it in an article I'm
> writing anyway.  It's simple and readable enough that it should not
> clutter up the article much.
> 
> I tested it for some simple use cases, and it works well.  Thanks for
> saving me a little trouble.
> 
> -- 
> Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]


You're most welcome.  Perhaps someone with more sh fu can transform the
'if' paragraph into a one-liner at least.  When I tried to do so, I got an
unexpected ; error.

-- 
Sterling (Chip) Camden    | sterling at camdensoftware.com | 2048D/3A978E4F
http://camdensoftware.com | http://chipstips.com        | http://chipsquips.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20101028/6e3a9d15/attachment.pgp


More information about the freebsd-questions mailing list