Scope of Variables in sh

Michael Schuh michael.schuh at gmail.com
Thu Jun 1 07:28:52 PDT 2006


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......

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.

thanks

michael


More information about the freebsd-stable mailing list