From owner-freebsd-stable@FreeBSD.ORG Thu Jun 1 15:46:23 2006 Return-Path: X-Original-To: freebsd-stable@freebsd.org Delivered-To: freebsd-stable@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4827716AE6C for ; Thu, 1 Jun 2006 15:46:23 +0000 (UTC) (envelope-from michael.schuh@gmail.com) Received: from wx-out-0102.google.com (wx-out-0102.google.com [66.249.82.193]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2420C43D64 for ; Thu, 1 Jun 2006 15:46:12 +0000 (GMT) (envelope-from michael.schuh@gmail.com) Received: by wx-out-0102.google.com with SMTP id i31so219203wxd for ; Thu, 01 Jun 2006 08:46:12 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=ReCsPimiVy/wUrWwEHX+Xb7oEpFwPON+H5IGFj4YT1sfsvcJRq571/IwqHy2yi9/GmADPYNxlz21ana7V1se2wukCmxHSuW7NKZ5KeCRoL8mxQsWPzaesD6qXprQGcnwJ/5pb++aB1Rs3axrG7OQN0RaW3IdeB55HU3jTPMj9Rw= Received: by 10.70.90.16 with SMTP id n16mr900650wxb; Thu, 01 Jun 2006 08:46:12 -0700 (PDT) Received: by 10.70.113.4 with HTTP; Thu, 1 Jun 2006 08:46:12 -0700 (PDT) Message-ID: <1dbad3150606010846i7fbbacaal131d9a926698281f@mail.gmail.com> Date: Thu, 1 Jun 2006 17:46:12 +0200 From: "Michael Schuh" To: "Bob Willcox" In-Reply-To: <20060601145434.GA54972@rancor.immure.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1dbad3150606010728l379e4c23p1a77558800498806@mail.gmail.com> <20060601145434.GA54972@rancor.immure.com> Cc: freebsd-hackers@freebsd.org, freebsd-stable@freebsd.org Subject: Re: Scope of Variables in sh X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Jun 2006 15:46:23 -0000 Hello Bob, Hello @Lists, > countobjects() > { > myline=0 > cat $objectcountfile|grep -v "^[^0-9]*$"|grep -v "^0$"| > awk '{myline += $1} END {print myline}' > $objectsum > } this gave me the right behavior. THX i ve forgotten that the piping opens another subshell, so it is clearly logical that the 2 variables have the same name, but are not identical :-) thanks for all michael 2006/6/1, Bob Willcox : > > On Thu, Jun 01, 2006 at 04:28:39PM +0200, Michael Schuh wrote: > > Hello, > > > > i have a little problem with the > > scope of sheel variables in an script. > > the script shows like: > > > > #!/bin/sh > > objectcountfile=/data2/scout/objects > > objectsum=/data2/scout/objcount > > sometime=5 > > initialize() > > { > > [ ! -e $objectsum ] && touch $objectsum > > } > > # > > countobjects() > > { > > myline=0 > > cat $objectcountfile|grep -v "^[^0-9]*$"|grep -v "^0$"| while read line > > do > > myline=`expr $myline + $line` > > echo $myline > $objectsum > > done > > } > > initialize > > while true > > do > > countobjects > > clear > > echo bla bla > > cat $objectsum > > sleep $sometime > > done > > ##end script > > > > this script does what i want, but, i dont really want put > > the count ($myline) every time he changes in $objectsum. > > this wasnt really neccessary only the result over all interests me. > > so my first script was > > ## > > countobjects() > > { > > myline=0 > > cat $objectcountfile|grep -v "^[^0-9]*$"|grep -v "^0$"| while read line > > do > > myline=`expr $myline + $line` > > done > > echo $myline > $objectsum > > } > > ## > > but this doesnt function right. so i see the behavior from $myline in the > > while-loop like an local variable...... > > With: > > while ... > do > ... > done > > The statements within the do...done sequence are run in a subshell and > therefore all variables referenced by those statements are local to > that subshell. Consequently, the myline variable that is outside of the > do...done sequence is a *different* variable (and will still be null in > your example). > > I'm not sure what your data looks like, but one possible solution would > be to use awk to read the lines and do the addition, perhaps like this: > > countobjects() > { > myline=0 > cat $objectcountfile|grep -v "^[^0-9]*$"|grep -v "^0$"| > awk '{myline += $1} END {print myline}' > $objectsum > } > > Note that I didn't actually run the above code, but I think it's > correct, and if not, it should be pretty close to what you need. > > Of course, you could replace the cat and 2 greps with some additional > awk stuff (and improve performance), but I wanted to keep it as simple > and close to your example as possible. > > > > > i have searched in man-pages and in google but i have > > not really good points found. > > > > Can anyone give me an ligthshed on this problem? > > Please answer me directly, i be out of freebsd-hackers. > > I recommend some good shell & awk programming books. > > Hope this helps, > Bob > > > > > thanks > > > > michael > > _______________________________________________ > > freebsd-stable@freebsd.org mailing list > > http://lists.freebsd.org/mailman/listinfo/freebsd-stable > > To unsubscribe, send any mail to "freebsd-stable-unsubscribe@freebsd.org" > > -- > Bob Willcox Grabel's Law: > bob@immure.com 2 is not equal to 3 -- not even for large values > Austin, TX of 2. >