You have a few issues.
First, why bother to test this?
todays_day=$(date +%d)
if ((todays_day==1)); then
month="$(date --date='1 day ago' +%y%m)"
else
month="$(date +%y%m)"
fi
I think all you need is
month="$(date --date='1 day ago' +%y%m)"
which accomplishes the same thing.
Second, your output is inside your loop, which isn't what you want - but since you are using a pipe, your variables will be scoped to the subshell of the loop.
Try this -
month="$(date --date='1 day ago' +%y%m)"
while read catalog
do sum=0
while read lines
do ((sum+=lines))
done < <( zgrep -ch . "$catalog"/*.z )
echo "no of lines $sum;source $catalog;data $(date +"%d-%m-%Y %T")"
done < <(find ./ -type d -name "$month" )
Note only one find
. If your example output can be trusted for a pattern, we could simplify the outer loop to
for catalog in ./AXE/*/"$month"
or, if there could be deeper more then one level of subdirectories between,
shopt -s globstar
for catalog in ./AXE/**/"$month"
which eliminates the need for the find
at the end entirely.
#!/bin/bash
month="$(date --date='1 day ago' +%y%m)" # sums previous month on the 1st
for catalog in ./AXE/*/"$month" # processes each $month dir
do sum=0 # reinit sum for each dir
while read lines # lines is the count per file
do ((sum+=lines)) # sum the files in the dir
done < <( zgrep -ch . "$catalog"/*.z ) # zgrep is your friend
echo "no of lines $sum;source $catalog;data $(date +"%d-%m-%Y %T")"
done
edit
As I said above,
or, if there could be deeper more then one level of subdirectories between,
shopt -s globstar
for catalog in ./AXE/**/"$month"
Substituting that in,
#!/bin/bash
shopt -s globstar # allow ** for arbitrary depth
month="$(date --date='1 day ago' +%y%m)" # sums previous month on the 1st
for catalog in ./AXE/**/"$month"/ # / at the end gets dirs only
do sum=0 # reinit sum for each dir
while read lines # lines is the count per file
do ((sum+=lines)) # sum the files in the dir
done < <( zgrep -ch . "${catalog}"*.z ) # / is part of $catalog
echo "no of lines $sum;source $catalog;data $(date +"%d-%m-%Y %T")"
done
The outer loop above selects applicable directories; the inner loop counts records in each file and sums by directories, which is what I thought you wanted.
In your answer post with your edited code, you have changed the outer loop to directly represent each individual file, so the inner loop is only reading one line, which leaves nothing to sum, so there's no need for an inner loop, but no sums by directory.
Did you want totals per file, per directory, or something else?
updated
Apologies. I included the *.z
glob inside the quotes. Corrected, above.
If there are no naughty characters such as spaces in any of the path names you could leave the quotes off entirely, but I tend not to make that assumption - it will surprise you.