說明
此腳本用於統計目錄中不同文件名,各類文件的數量。比如: E9020_1111 與 E9020_2222是一類文件,但E9030_1111 與 E9030_2222又是另一類文件,統計其數量。
dir_list
表示目錄列表,static
表示統計函數。
- 如果
dir
存在,且目錄中有文件,則進行統計;- 將統計信息保存在一個map[filename,count]中,其中key是文件名的前5個字符,value是文件數量。
腳本:
#!/bin/bash
# statistic some direcotrys
dir_list=(
/app/billapp/data/sg_005_1/err
/app/billapp/data/sg_005_1/ok
#/app/billapp/data/cg/err
#/app/billapp/data/rf_005/err
#/app/billapp/data/rf_006/err
#/app/billapp/data/rf_007/err
#/app/billapp/data/rf_008/err
#/app/billapp/data/cg/ok
#/app/billapp/data/rf_005/ok
#/app/billapp/data/rf_006/ok
#/app/billapp/data/rf_007/ok
#/app/billapp/data/rf_008/ok
)
# Statistics the number of various filenames in the directory
function static {
if [ $# -ne 1 ]
then
return 1
fi
local directory=$1
declare -A local mycount # map[filename, count]
contents=`ls -A $directory`
for file in $contents
do
if [ -f $directory/$file ]
then
mycount[${file:0:4}]=$[ ${mycount[${file:0:4}]} + 1 ]
fi
done
for key in ${!mycount[@]}
do
echo "$key : ${mycount[$key]}"
done
return 0
}
for dir in ${dir_list[@]}
do
echo $dir
# If the directory exists and there are some files in the directory, then statistic.
if [ -d $dir ] && [ "`ls -l $dir | grep "^-" `" != "" ]
then
static $dir
else
echo "The $dir doesn't exist or there are no files in it."
fi
echo
done
exit 0