From owner-freebsd-questions@FreeBSD.ORG Thu Oct 28 15:11:54 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 740211065672 for ; Thu, 28 Oct 2010 15:11:54 +0000 (UTC) (envelope-from sterling@camdensoftware.com) Received: from wh2.interactivevillages.com (wh2.interactivevillages.com [75.125.250.34]) by mx1.freebsd.org (Postfix) with ESMTP id 3C2A68FC18 for ; Thu, 28 Oct 2010 15:11:53 +0000 (UTC) Received: from 174-21-106-233.tukw.qwest.net ([174.21.106.233] helo=_HOSTNAME_) by wh2.interactivevillages.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1PBU0b-00066T-L8 for freebsd-questions@freebsd.org; Thu, 28 Oct 2010 08:03:18 -0700 Received: by _HOSTNAME_ (sSMTP sendmail emulation); Thu, 28 Oct 2010 08:11:48 -0700 Date: Thu, 28 Oct 2010 08:11:48 -0700 From: Chip Camden To: FreeBSD Questions Message-ID: <20101028151148.GB73337@libertas.local.camdensoftware.com> Mail-Followup-To: FreeBSD Questions References: <20101027212841.GA67716@guilt.hydra> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="BwCQnh7xodEAoBMC" Content-Disposition: inline In-Reply-To: <20101027212841.GA67716@guilt.hydra> User-Agent: Mutt/1.4.2.3i Company: Camden Software Consulting URL: http://camdensoftware.com X-PGP-Key: http://pgp.mit.edu:11371/pks/lookup?search=0xD6DBAF91 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - wh2.interactivevillages.com X-AntiAbuse: Original Domain - freebsd.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - camdensoftware.com 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 15:11:54 -0000 --BwCQnh7xodEAoBMC Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Quoth Chad Perrin on Wednesday, 27 October 2010: > I know that in sh you can get the contents out of files specified as > command line arguments: >=20 > while read data; do > echo $data > done <$@ >=20 > I know you can also get the contents of files from pipes and redirects: >=20 > while read data; do > echo $data > done >=20 > In Perl, you can use a single construct to do both and, unlike the first > sh example, it can also take multiple filenames as arguments and > effectively concatenate their contents: >=20 > while (<>) { > print $_; > } >=20 > I'm not exactly an *expert* in sh, in part because when things start > getting "interesting" while I'm writing shell scripts I tend to just use > a more robust language like Perl. Please let me know if there's some way > to use a simple idiom like the Perl example to get the same results in > sh. >=20 > --=20 > Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] 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. --=20 Sterling (Chip) Camden | sterling@camdensoftware.com | 2048D/3A978E4F http://camdensoftware.com | http://chipstips.com | http://chipsquips= .com --BwCQnh7xodEAoBMC Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (FreeBSD) iQEcBAEBAgAGBQJMyZK0AAoJEIpckszW26+Rd/AIAKpDWnVpP+SDncbG0ClSdY8v 47Cr0QTZxEVuO4TaXkTLXrW+VtHXwYA13XLGfRaDiS5ocQzHRkV4UL3ffQXRR+5b XtjfirVAJissoW9wgKaw+7YxjVlRVv1+Qx8enRUBx3tRIFZG4fIdY6XRuIf5dHBz Npr8X64W8ymljd/65q04pMB3jdqC9Ui6kl3w7cWt1BsU/ECiLfFW0AGfCxpikGWt HbdtfNO2ydJnvTpJA2fpG7J48PxoDk8HFLs1XCEOCnuWg35TvcT3Razv8Awj79tA X5UAKSZ2sXjcArgEkVZN6DOURVRJmx1tb5jC4N5cwiUmMNRBbBZ7JmAibL+RXyI= =O6v7 -----END PGP SIGNATURE----- --BwCQnh7xodEAoBMC--