Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Oct 2010 10:49:03 -0700
From:      Chip Camden <sterling@camdensoftware.com>
To:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: [OT] writing filters in sh
Message-ID:  <20101028174903.GE73337@libertas.local.camdensoftware.com>
In-Reply-To: <20101028192826.d3211524.freebsd@edvax.de>
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>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
Quoth Polytropon on Thursday, 28 October 2010:
> On Thu, 28 Oct 2010 09:17:12 -0700, Chip Camden <sterling@camdensoftware.com> 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.
> 
> Not tested, but this should do the trick:
> 
> 	#!/bin/sh
> 	if [ $# -ge 1 ] && ( exec cat $@ | $0; exit )
> 	while read data; do echo $data; done
> 
> The ; denotes a line break, means that you can use EITHER ; or
> a newline. In the original construct, 
> 
> 	if [ $# -ge 1 ]; then
> 		exec cat $@ | $0
> 		exit
> 	fi
> 
> you can change the ; into
> 
> 	if [ $# -ge 1 ]
> 	then
> 		exec cat $@ | $0
> 		exit
> 	fi
> 
> 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.
> 
> 
> -- 
> 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.org"

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='[ $# -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.

-- 
Sterling (Chip) Camden    | sterling@camdensoftware.com | 2048D/3A978E4F
http://camdensoftware.com | http://chipstips.com        | http://chipsquips.com

[-- Attachment #2 --]
-----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-----
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20101028174903.GE73337>