<p>When I supply the script with the argument: hi[123].txt it will do exactly what I want.
But if I specify the wildcard character ( hi*.txt ) it will be re-reading some files.</p>

<p>I was wondering how to modify this script to fix that silly problem:</p>

<pre><code>#!/bin/sh

count="0"
total="0"
FILE="$1"  #FILE specification is now $1 Specification..

for FILE in $@
do
  #if the file is not readable then say so
     if [ ! -r $FILE ];
         then
         echo "File: $FILE not readable"
     exit 0
     fi


# Start processing readable files
  while read line
          do

             if [[ "$line" =~ ^Total ]];
                 then
                   tmp=$(echo $line | cut -d':' -f2)

                   total=$(expr $total + $tmp)

                   echo "$FILE (s) have a total of:$tmp "
                   count=$(expr $count + 1)

             fi

        done &lt; $FILE
done
echo " Total is: $total"
echo " Number of files read is:$count"
</code></pre>

I don't know what is wrong with it, but one little point i noticed:
Change
```
for FILE in $@
```
into
```
for FILE in "$@"
```
. Because if files have embedded spaces, you are now on the safe way. It will expand into
```
"$1" "$2" ...
```
then, instead of
```
$1 $2 ...
```
(and note everywhere you use $FILE too remember to
```
""
```
it).
And what others say, you don't need to initialize
```
FILE
```
before you enter the loop. It will be set to each of the filenames of the expanded positional parameters in the for loop automatically.
However, i would go with an awk script like this:
```
```
awk -F: '
/^Total/ { 
    total += $2
    # count++ not needed. see below
    print FILENAME "(s) have a total of: " $2
} 

END { 
    print "Total is: " total
    print "Number of files read is: " (ARGC-1) 
}' foo*.txt
```
```
Note that when a file contains multiple "^Count" lines, you would indeed say you read more files than you actually read if you rely on
```
count
```
to tell you the number of files read.