[OT] writing filters in sh

Karl Vogel vogelke+unix at pobox.com
Thu Oct 28 17:53:04 UTC 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"


More information about the freebsd-questions mailing list