Scope of Variables in sh

Michael Schuh michael.schuh at gmail.com
Thu Jun 1 08:46:23 PDT 2006


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 at 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 at freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-stable
> > To unsubscribe, send any mail to "freebsd-stable-unsubscribe at freebsd.org"
>
> --
> Bob Willcox            Grabel's Law:
> bob at immure.com            2 is not equal to 3 -- not even for large values
> Austin, TX                of 2.
>


More information about the freebsd-stable mailing list