From owner-freebsd-questions@FreeBSD.ORG Mon Aug 18 01:47:21 2008 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 B395A106568D for ; Mon, 18 Aug 2008 01:47:21 +0000 (UTC) (envelope-from david@catwhisker.org) Received: from bunrab.catwhisker.org (adsl-63-193-123-122.dsl.snfc21.pacbell.net [63.193.123.122]) by mx1.freebsd.org (Postfix) with ESMTP id 3690F8FC1D for ; Mon, 18 Aug 2008 01:47:21 +0000 (UTC) (envelope-from david@catwhisker.org) Received: from bunrab.catwhisker.org (localhost [127.0.0.1]) by bunrab.catwhisker.org (8.13.3/8.13.3) with ESMTP id m7I1XSiv094137; Sun, 17 Aug 2008 18:33:28 -0700 (PDT) (envelope-from david@bunrab.catwhisker.org) Received: (from david@localhost) by bunrab.catwhisker.org (8.13.3/8.13.1/Submit) id m7I1XSSm094136; Sun, 17 Aug 2008 18:33:28 -0700 (PDT) (envelope-from david) Date: Sun, 17 Aug 2008 18:33:28 -0700 From: David Wolfskill To: freebsd-questions@freebsd.org Message-ID: <20080818013328.GY44815@bunrab.catwhisker.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="rLeWyzpnKP7Xancb" Content-Disposition: inline User-Agent: Mutt/1.4.2.1i Subject: Shell scripts: variable assignment within read loops X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: David Wolfskill , freebsd-questions@freebsd.org List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Aug 2008 01:47:21 -0000 --rLeWyzpnKP7Xancb Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I am writing a (Bourne) shell script that is intended (among other things) to obtain information from a command, such as: netstat -nibd -f inet by reading and parsing the output. However, the "obvious" (to me) approach of piping the output of the command to the standard input of a "while read ..." statement turns out to be not very useful; it appears that while foo=3D"" while read bar ... ; do ... foo=3D$bar ... done <$filename echo $foo will assign to foo the value of the bar variable form the last record read (in FreeBSD 6.3-STABLE, at least), the following fails to do so: foo=3D"" cat $filename | while read bar ... ; do ... foo=3D$bar ... done echo $foo Well, that's not *quite* accurate:the assignment is done all right, but in the latter case, it appears to be done in a subshell, so by the time we get to the "echo" statement, any variable assignments from within the read loop have vanished. Here's a cut/pasted, somewhat contrived example: #! /bin/sh foo=3D0 echo "0 foo: $foo" while read line; do echo "1.0 foo: $foo" foo=3D1$line echo "1.1 foo: $foo" done $t_file while read $hlist dummy; do if [ "$Name" =3D "lo0" ]; then continue fi for f in $hlist; do eval val=3D\"\$$f\" case $val in -) eval ${f}_$Name=3D0;; *) eval ${f}_$Name=3D"$val";; esac; done nics=3D"$Name $nics"; done