Date: Thu, 1 Jun 2006 17:46:12 +0200 From: "Michael Schuh" <michael.schuh@gmail.com> To: "Bob Willcox" <bob@immure.com> Cc: freebsd-hackers@freebsd.org, freebsd-stable@freebsd.org Subject: Re: Scope of Variables in sh Message-ID: <1dbad3150606010846i7fbbacaal131d9a926698281f@mail.gmail.com> In-Reply-To: <20060601145434.GA54972@rancor.immure.com> References: <1dbad3150606010728l379e4c23p1a77558800498806@mail.gmail.com> <20060601145434.GA54972@rancor.immure.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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 <bob@immure.com>: > > 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. >
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1dbad3150606010846i7fbbacaal131d9a926698281f>