#! /bin/bash
FILE=”[aeiou]”
declare -a ARRAY
COUNT=$( grep “$FILE” /var/log/httpd/access_log |wc -l )
ARRAY=( “$COUNT” )
echo “count of array elements: ${#ARRAY[@]}”
echo ” list of array elements: ${ARRAY[@]}”
ls /var/log/httpd/access_log.[0-9].gz |
{
while read LOG ; do
COUNT=$( gzcat “$LOG” |grep “$FILE” |wc -l )
ARRAY=( ${ARRAY[@]} “$COUNT” )
echo “count of array elements: ${#ARRAY[@]}”
echo ” list of array elements: ${ARRAY[@]}”
done
}
echo “count of array elements: ${#ARRAY[@]}”
echo ” list of array elements: ${ARRAY[@]}”
for value in ${ARRAY[@]} ; do
SUM=$(( $SUM + $value ))
done
echo “$SUM”
############################
$ bash -x this_script
+ FILE=[aeiou]
+ declare -a ARRAY
++ grep ‘[aeiou]’ /var/log/httpd/access_log
++ wc -l
+ COUNT= 1308
+ ARRAY=(“$COUNT”)
+ echo ‘count of array elements: 1’
count of array elements: 1
+ echo ‘ list of array elements: 1308’
list of array elements: 1308
+ ls /var/log/httpd/access_log.2.gz /var/log/httpd/access_log.4.gz
+ read LOG
++ gzcat /var/log/httpd/access_log.2.gz
++ grep ‘[aeiou]’
++ wc -l
+ COUNT= 240
+ ARRAY=(${ARRAY[@]} “$COUNT”)
+ echo ‘count of array elements: 2’
count of array elements: 2
+ echo ‘ list of array elements: 1308’ ‘ 240’
list of array elements: 1308 240
+ read LOG
++ gzcat /var/log/httpd/access_log.4.gz
++ grep ‘[aeiou]’
++ wc -l
+ COUNT= 1429
+ ARRAY=(${ARRAY[@]} “$COUNT”)
+ echo ‘count of array elements: 3’
count of array elements: 3
+ echo ‘ list of array elements: 1308’ 240 ‘ 1429’
list of array elements: 1308 240 1429
+ read LOG
+ echo ‘count of array elements: 1’ # <— why
does it go back to 1 element here?
count of array elements: 1
+ echo ‘ list of array elements: 1308’ # <– why
does $ARRAY lose elements 2 and 3 here?
list of array elements: 1308
+ SUM=1308
+ echo 1308
1308
Thx.
.
실제 내가 사용하는 부분 배열의 합
array=( “$B” )
#echo “${array[@]}”
for value in ${array[@]}
do
SUM=$(( $SUM + $value ))
done
#echo $SUM