From owner-freebsd-questions@FreeBSD.ORG Thu Oct 28 18:08:18 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1656E10656EB for ; Thu, 28 Oct 2010 18:08:18 +0000 (UTC) (envelope-from dteske@vicor.com) Received: from postoffice.vicor.com (postoffice.vicor.com [69.26.56.53]) by mx1.freebsd.org (Postfix) with ESMTP id F11228FC0A for ; Thu, 28 Oct 2010 18:08:17 +0000 (UTC) Received: from [208.206.78.30] (port=51029 helo=dt.vicor.com) by postoffice.vicor.com with esmtpsa (SSLv3:RC4-MD5:128) (Exim 4.71) (envelope-from ) id 1PBWtb-0005c6-4P; Thu, 28 Oct 2010 11:08:17 -0700 From: Devin Teske To: Chip Camden In-Reply-To: <20101028161712.GC73337@libertas.local.camdensoftware.com> References: <20101027212841.GA67716@guilt.hydra> <20101028151148.GB73337@libertas.local.camdensoftware.com> <20101028160110.GA71713@guilt.hydra> <20101028161712.GC73337@libertas.local.camdensoftware.com> Content-Type: text/plain Organization: Vicor, Inc Date: Thu, 28 Oct 2010 11:08:14 -0700 Message-Id: <1288289294.20243.31.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-41.el4) Content-Transfer-Encoding: 7bit X-Scan-Signature: 7e3de7e3382d5c42a9a7cfa0a3636125 X-Scan-Host: postoffice.vicor.com Cc: FreeBSD Questions Subject: Re: [OT] writing filters in sh X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Oct 2010 18:08:18 -0000 On Thu, 2010-10-28 at 09:17 -0700, Chip Camden wrote: > 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. Here you go: alias myfilter="/bin/sh -c '"'myfilter() { if [ $# -gt 0 ]; then while [ $# -gt 0 ]; do [ -f "$1" ] && myfilter < "$1"; shift 1; done; else while read LINE; do : some transform ; echo $LINE; done; fi; }; myfilter "$@"'"' -- /bin/sh" The above is to be entered as a single command-line. NOTE: If you're using either csh or tcsh, you only need to make one change... change "alias myfilter=..." to instead "alias myfilter ..." (that's right, just change the equals sign to a space). Here's a demo: $ printf "Line 1\nLine 2\nLine 3\n" | myfilter Line 1 Line 2 Line 3 $ head -2 /etc/rc.conf | myfilter # This file now contains just the overrides from /etc/defaults/rc.conf # please make all changes to this file. $ myfilter < /etc/rc.conf | head -2 # This file now contains just the overrides from /etc/defaults/rc.conf # please make all changes to this file. $ cat /etc/rc.conf | myfilter | head -2 # This file now contains just the overrides from /etc/defaults/rc.conf # please make all changes to this file. $ myfilter > somefile Line 1 Line 2 Line 3 ^D $ cat somefile Line 1 Line 2 Line 3 > > > > 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. > -- Cheers, Devin Teske -> CONTACT INFORMATION <- Business Solutions Consultant II FIS - fisglobal.com 510-735-5650 Mobile 510-621-2038 Office 510-621-2020 Office Fax 909-477-4578 Home/Fax devin.teske@fisglobal.com -> LEGAL DISCLAIMER <- This message contains confidential and proprietary information of the sender, and is intended only for the person(s) to whom it is addressed. Any use, distribution, copying or disclosure by any other person is strictly prohibited. If you have received this message in error, please notify the e-mail sender immediately, and delete the original message without making a copy. -> END TRANSMISSION <-