[OT] writing filters in sh

Polytropon freebsd at edvax.de
Thu Oct 28 17:28:29 UTC 2010


On Thu, 28 Oct 2010 09:17:12 -0700, Chip Camden <sterling at 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, ...


More information about the freebsd-questions mailing list