initiate () {
read -p "Location(s) to look for .bsp files in? " loc
find $loc -name "*.bsp" | while read
do
if [ -f "$loc.bz2" ]
then
continue
else
filcount=$[$filcount+1]
bzip $loc
fi
if [ "$scan" == "1" ]; then bzipint $loc
fi
echo $filcount #Correct counting
echo $zipcount #Correct counting
echo $scacount #Correct counting
echo $valid #Equal to 1
done
echo $filcount #Reset to 0
echo $zipcount #Reset to 0
echo $scacount #Reset to 0
echo $valid #Still equal to 1
}
I'm writing a bash shell script to use bzip2
to zip up all .bsp
files inside a directory. In this script I have several variables for counting totals (files, successful zips, successful integrity scans), however I seem to have run into a problem.
When find $loc -name "*.bsp"
runs out of files to give the while read
and while read
exits, it zeros out $filcount
, $zipcount
and $scacount
(all of which are changed (increased) inside initiate ()
, bzip ()
(which is called during initiate ()
) or bzipint ()
(which is also called in initiate ()
).
In order to test if it's something to do with variables changing inside initiate ()
or other functions accessed from it, I used echo $valid
, which is defined outside of initiate ()
(like $filcount
, $zipcount
, etc.), but is not changed from another function inside initiate ()
or inside initiate ()
itself.
Interestingly enough, $valid
does not get reset to 0 like the other variables inside initiate.
Can anyone tell me why my variables magically get reset when while read exits?