From owner-freebsd-questions@FreeBSD.ORG Thu Oct 28 17:49:09 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 36D81106564A for ; Thu, 28 Oct 2010 17:49:09 +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 EE9098FC16 for ; Thu, 28 Oct 2010 17:49:08 +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 1PBWSm-0005pY-5M for freebsd-questions@freebsd.org; Thu, 28 Oct 2010 10:40:33 -0700 Received: by _HOSTNAME_ (sSMTP sendmail emulation); Thu, 28 Oct 2010 10:49:03 -0700 Date: Thu, 28 Oct 2010 10:49:03 -0700 From: Chip Camden To: FreeBSD Questions Message-ID: <20101028174903.GE73337@libertas.local.camdensoftware.com> Mail-Followup-To: FreeBSD Questions References: <20101027212841.GA67716@guilt.hydra> <20101028151148.GB73337@libertas.local.camdensoftware.com> <20101028160110.GA71713@guilt.hydra> <20101028161712.GC73337@libertas.local.camdensoftware.com> <20101028192826.d3211524.freebsd@edvax.de> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="n+lFg1Zro7sl44OB" Content-Disposition: inline In-Reply-To: <20101028192826.d3211524.freebsd@edvax.de> 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 17:49:09 -0000 --n+lFg1Zro7sl44OB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Quoth Polytropon on Thursday, 28 October 2010: > On Thu, 28 Oct 2010 09:17:12 -0700, Chip Camden wrote: > > 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. >=20 > Not tested, but this should do the trick: >=20 > #!/bin/sh > if [ $# -ge 1 ] && ( exec cat $@ | $0; exit ) > while read data; do echo $data; done >=20 > The ; denotes a line break, means that you can use EITHER ; or > a newline. In the original construct,=20 >=20 > if [ $# -ge 1 ]; then > exec cat $@ | $0 > exit > fi >=20 > you can change the ; into >=20 > if [ $# -ge 1 ] > then > exec cat $@ | $0 > exit > fi >=20 > which is often seen in scripts. In this case, no ; is required (or > even allowed). The same rule applies for the while/do/done iterator. >=20 >=20 > --=20 > Polytropon > Magdeburg, Germany > Happy FreeBSD user since 4.0 > Andra moi ennepe, Mousa, ... > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.o= rg" Tested, and it had a bug ('if' is extraneous). It also waits for input even if it has file arguments. But it's easily corrected: #!/bin/sh [ $# -ge 1 ] && exec cat $@ && exit | $0 while read data; do echo $data; done The first line can be converted to an alias, like so: alias inargs=3D'[ $# -ge 1 ] && exec cat $@ && exit | $0' So, if you add the alias to your profile, you can use it to enable the behavior in any script: #!/bin/sh inargs while read data; do echo $data; done Thanks for the pointer in the right direction. --=20 Sterling (Chip) Camden | sterling@camdensoftware.com | 2048D/3A978E4F http://camdensoftware.com | http://chipstips.com | http://chipsquips= .com --n+lFg1Zro7sl44OB Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (FreeBSD) iQEcBAEBAgAGBQJMybePAAoJEIpckszW26+RIQwH/0go9drdkfVf/VNy5zJBgq+5 aTUMV1OHhrKccffIyEoFLDAg36i31IwR+2evqvwPkGc0Vjq0RHEdmlPX75CN9RWl CKv6qdrKY8qRuLRYu66uGfJ1cFbKon71tAqAIf02iWvAa5/8b8oiskzThKWlgxlM CCPCS/fx+KNC5FsmdBDw1ySo0rCQ26M3kzjFnQBlSvIuznUDHKGIWV2Y0/nNaMII hoK6WGNCPEuTMEEf/aSUyjjdVoagXyER4UVGTW19nWq68MA2kfJLulZKcQFYIh8c Xprne3741frariUbPxhfWt9auY4aUJcQaI4yrAw5Jk9n8tx+MDeGcSv4gruIA8I= =3lXs -----END PGP SIGNATURE----- --n+lFg1Zro7sl44OB--