shell脚本遍历子目录

时间:2023-01-20 09:12:13
 
 

#!/bin/bash
source /etc/profile

 
 

tool_path=/data/rsync_client
root_path=/data/log

 
 

###
#yyyy-mm-dd¸ñʽ
date_today=`date +"%Y-%m-%d"`
date_yesterday=`date -d "yesterday" +%Y-%m-%d`

 
 

#yyyymmdd¸ñʽ
date_today_hadoop=`date +"%Y%m%d"`
date_yesterday_hadoop=`date -d "yesterday" +%Y%m%d`

 
 

#yyyymd¸ñʽ
file_today=${date_today//-0/-}
file_yesterday=${date_yesterday//-0/-}

 
 


### kill rsync
${tool_path}/killrsync.sh

 
 

###
destFile_yesterday="${tool_path}/tlog/${date_yesterday_hadoop}"
if [[ ! -d ${destFile_yesterday} ]]; then
mkdir -p ${destFile_yesterday}
fi

 
 

destFile_today="${tool_path}/tlog/${date_today_hadoop}"
if [[ ! -d ${destFile_today} ]]; then
mkdir -p ${destFile_today}
fi

 
 

## ÿÌìÉú³ÉÒ»¸öÎļþ£¬Ã»¹ýÂ˵ôZKMONITORµÄÈÕÖ¾£¬ÀïÃæÒ²´øÁË|
## cat ${root}/*.${file_yesterday} | grep '20..-.*LOG:.*|.*' | awk '{$1="";$2="";$3="";$4="";sub(" LOG:", "");print}' > ${destFile_yesterday}/tlog_${date_yesterday_hadoop}.log
## cat ${root}/*.${file_today} | grep '20..-.*LOG:.*|.*' | awk '{$1="";$2="";$3="";$4="";sub(" LOG:", "");print}' > ${destFile_today}/tlog_${date_today_hadoop}.log

 
 

cd ${root_path}
list_alldir(){
for file2 in `ls -a $1`
do
if [ x"$file2" != x"." -a x"$file2" != x".." ];then
if [ -d "$1/$file2" ];then
echo "$1/$file2"
cd $1/${file2}
cat $1/${file2}/*.${file_yesterday} | grep '20..-.*LOG:.*|.*' | awk '{$1="";$2="";$3="";$4="";sub(" LOG:", "");print}' > ${destFile_yesterday}/tlog_${date_yesterday_hadoop}_${file2}.log
cat $1/${file2}/*.${file_today} | grep '20..-.*LOG:.*|.*' | awk '{$1="";$2="";$3="";$4="";sub(" LOG:", "");print}' > ${destFile_today}/tlog_${date_today_hadoop}_${file2}.log

 
 

list_alldir "$1/${file2}"
fi
fi
done
}

list_alldir ${root_path}

 
 


cd ${destFile_yesterday}
find . -name "*" -type f -size 0c | xargs -n 1 rm -f
cd ${destFile_today}
find . -name "*" -type f -size 0c | xargs -n 1 rm -f

 
 


### start rsync
flock -xn ${tool_path}/rsync.lock -c "${tool_path}/start.sh"

 

注意:

1、一定要使用绝对路径

2、定义变量时等号两边加空格会识别不出来。