bash에서 배열 사용

#! /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




코멘트 쓰기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.

다음의 HTML 태그와 속성을 사용할 수 있습니다:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



배송정보
배송조회를 하시려면 송장번호를 클릭하세요
배송조회
상품명
주문번호
택배사
송장번호