Hi All,
I wrote the below bash code which will count the number of times a specific keyword(macro) is found in the list of files(in recursive fashion). But what happens is when I execute the script in "echo appears_1" the $CurrFileTotalCount and $TotalCount prints properly BUT in "echo appears_2" the value of $TotalCount is displayed as 0.
Code:
#!/usr/local/bin/bash -x
TotalCount=0
#takes One $macro after other from macro_names.txt file
for macro in $(cat macro_names.txt);
do
#Takes $files from the current path in a recursive manner
find . -type f -name "*.c" -o -name "*.h" | while read files
do
#Counts the number of times $macro is present in $files
CurrFileTotalCount=$(cat "${files}" | grep $macro | wc -l);
#Adds the Current file count to $TotalCount
TotalCount=$(($TotalCount+$CurrFileTotalCount));
#print the $CurrFileTotalCount and $TotalCount PROPERLY
echo $macro appears_1 CurrFileTotalCount $CurrFileTotalCount TotalCount $TotalCount
done
#Expected to print how many times that $macro is present in the files -- BUT PRINTS $TotalCount AS 0
echo $macro appears_2 TotalCount $TotalCount
TotalCount=0;
done
Above is the code I think the scope is the problem I tried using "declare -i" but it is also not working. Can you please point out what is wrong with the script.