Hi,
I was having a problem regarding this thread:
http://www.unix.com/shell-programming-scripting/225221-setting-variable-using-variables-loop.html
I have not been able to solve that issue. So i was thinking maybe my logic to solve the overall problem is wrong, so i am looking for a bit of help on what would be a good way to approach the following task.
I have a list of 24 files that are produced daily. On some days all 24 files may not be produced. The filenames are in the format filename-2digitnumber ( e.g. log-01). In each of the files, i need to search for an occurrence of a particular word and see how many times it occurs. Once i have the occurrences per file, i then need to total up all the occurrences per file into a grand total.
The way i have gone about this was to do a for loop, which had an if statement within it which did a check to see if the file existed. Then I would do a count on the file and assign it to a variable, that is also a variable. I get stuck at this stage as in the above thread. Code example:
file=log
for i in 01 02 03; do
if [ -f $file-$i ]; then
count$i=`grep -c word $file-$i`
fi
done
for i in 01 02 03; do
if [ ! -z "$count$i" ]; then
:
else Tcount=`expr $Tcount + $count$i`
fi
done
Any ideas on how to go about this will be appreciated.
Thanks