[OT] writing filters in sh

Chip Camden sterling at camdensoftware.com
Thu Oct 28 17:59:46 UTC 2010


Quoth Karl Vogel on Thursday, 28 October 2010:
> >> On Thu, 28 Oct 2010 09:17:12 -0700, 
> >> Chip Camden <sterling at camdensoftware.com> said:
> 
> C> Perhaps someone with more sh fu can transform the 'if' paragraph into a
> C> one-liner at least.  When I tried to do so, I got an unexpected ; error.
> 
>    Try this:
> 
>      #!/bin/sh
>      test $# -ge 1 && cat $@ | exec $0 && exit 0
> 
>      IFS=$'\n'
>      while read data; do
>        echo "$data"
>      done
>      exit 0
> 
>    Resetting IFS is only necessary if you want to preserve whitespace in
>    each line.  Putting $data in quotes prevents any shell metacharacters
>    from being expanded.
> 
> -- 
> Karl Vogel                      I don't speak for the USAF or my company
> Sign in a New York drugstore: "We dispense with accuracy"
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"


Much better than my latest version, which always invokes $0.  As an
alias:

alias inargs='[ $# -ge 1 ] && cat $@ | exec $0 && exit'

#!/bin/sh
inargs
while read data; do echo $data; done

-- 
Sterling (Chip) Camden    | sterling at camdensoftware.com | 2048D/3A978E4F
http://camdensoftware.com | http://chipstips.com        | http://chipsquips.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20101028/43694db5/attachment.pgp


More information about the freebsd-questions mailing list