Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counter using Bourne Shell 1

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello all,

In a Bourne shell script on Solaris 8 I am attempting to use the expr command to implement a counter in my script. The problem is my COUNTER variable seems to lose it's value after the loop. While in the loop the COUNTER works as it echos out incrementing values properly.

Here is my script snippet:
Code:
COUNTER=0
while read LINE
do
echo Processing: $LINE
   [COLOR=green]# The below line displays the correct value[/color]
   COUNTER=`expr $COUNTER + 1`
   echo $COUNTER
done < /tmp/items.lst

[COLOR=green]# The below line displays [b]0[/b] ????!!![/color]
echo $COUNTER

What can you recommend?



Thanks,

Michael42
 
The reason why is that the while loop is executed in a subshell due the i/o redirection.
You may try something like this:
exec </tmp/items.lst
while read LINE
...
done
echo $COUNTER

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV,

Perfect! Thanks for taking the time to post. :)

-Michael42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top