From owner-freebsd-questions@FreeBSD.ORG Thu Oct 28 02:05:54 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 E38BE1065670 for ; Thu, 28 Oct 2010 02:05:54 +0000 (UTC) (envelope-from vogelke@hcst.net) Received: from beta.hcst.com (beta.hcst.com [192.52.183.241]) by mx1.freebsd.org (Postfix) with ESMTP id A97018FC15 for ; Thu, 28 Oct 2010 02:05:54 +0000 (UTC) Received: from beta.hcst.com (localhost [127.0.0.1]) by beta.hcst.com (8.14.3/8.14.3/Debian-5+lenny1) with ESMTP id o9S25rGG030902 for ; Wed, 27 Oct 2010 22:05:53 -0400 Received: (from vogelke@localhost) by beta.hcst.com (8.14.3/8.14.3/Submit) id o9S25r8h030901; Wed, 27 Oct 2010 22:05:53 -0400 Received: by kev.msw.wpafb.af.mil (Postfix, from userid 32768) id A282DBF80; Wed, 27 Oct 2010 22:04:52 -0400 (EDT) To: freebsd-questions@freebsd.org In-reply-to: <20101027212841.GA67716@guilt.hydra> (message from Chad Perrin on Wed, 27 Oct 2010 15:28:41 -0600) Organization: Array Infotech X-Disclaimer: I don't speak for the USAF or Array Infotech. X-GPG-ID: 1024D/711752A0 2006-06-27 Karl Vogel X-GPG-Fingerprint: 56EB 6DBF 4224 C953 F417 CC99 4C7C 7D46 7117 52A0 Message-Id: <20101028020452.A282DBF80@kev.msw.wpafb.af.mil> Date: Wed, 27 Oct 2010 22:04:52 -0400 (EDT) From: vogelke+unix@pobox.com (Karl Vogel) Subject: Re: [OT] writing filters in sh X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: vogelke+unix@pobox.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Oct 2010 02:05:55 -0000 >> On Wed, 27 Oct 2010 15:28:41 -0600, >> Chad Perrin said: C> In Perl, you can use a single construct to [read files or pipes], and it C> can also take multiple filenames as arguments and effectively C> concatenate their contents: C> while (<>) { print $_; } C> Please let me know if there's some way to use a simple idiom like the C> Perl example to get the same results in sh. AFAIK, you can easily do *one* input and output file, but not multiple input files like the Perl "diamond" operator unless you want to play games with the argument list. This works with sh, ksh, and bash on FreeBSD and Solaris: #!/bin/sh # filter: handle input/output files or stdin/stdout. # usage: filter [input] [output] # ie: filter read stdin, write stdout # filter in read file "in", write stdout # filter in out read file "in", write file "out" # filter - out read stdin, write file "out" PATH=/usr/local/bin:/bin:/usr/bin; export PATH case "$1" in "") ;; "-") shift ;; *) exec <"$1"; shift ;; esac case "$1" in "") ;; *) exec >"$1" ;; esac wc # or whatever exit 0 -- Karl Vogel I don't speak for the USAF or my company We seem not to have learned a basic lesson of history: Capitalism harnesses self-interest; socialism exhausts itself trying to kill it. --Linda Bowles