clean_up_log.sh

gself發表於2019-02-22
#!/usr/bin/env bash
# Name          : /etc/sh/clean_up_log.sh
# Author        : RaymondYan
# Create Date   : 2018-06-23
# Usage         : bash ./clean_up_log.sh {config_file} [specified_line_number]
# Example       : /bin/bash /etc/sh/clean_up_log.sh /huishoubao/config/clean_up_log.config 1
# Dependencies  : /etc/sh/lib/logToFile.sh, /huishoubao/config/clean_up_log.config
# Description   : Find log files and delete, depends on settings as setted by /huishoubao/config/clean_up_log.config.
# Version       : 1.0
# Update Date   :

LOG_FILE='/data/log/clean_up_log.log'
mkdir -pv /data/log/
source /etc/sh/lib/logToFile.sh
if [[ $? -ne 0 ]]; then echo -e "\033[41;37m [/etc/sh/lib/logToFile.sh] is necessary!! \033[0m"; exit 1; fi

#source /huishoubao/config/clean_up_log.config
#if [[ $? -ne 0 ]]; then echo -e "\033[41;37m [/huishoubao/config/clean_up_log.config] is necessary!! \033[0m"; exit 1; fi

n_inputf=$1
n_no=$2

while read line
do
  # ignore comment lines and blank lines
  if echo ${line} | grep -e '^#' -e '^$' >/dev/null; then
    continue
  fi

  # if there are line numbers specified, ignore except specified lines
  if [ -n ${n_no} -a "`echo $line | awk '{print $1}'`" != ${n_no} ]; then
    continue
  fi

 # below is main program

 n_profile=`echo ${line} | awk '{print $2}'`
 n_log_dir=`echo ${line} | awk '{print $3}'`
 n_log_wildcards=`echo ${line} | awk '{print $4}'`
 n_days_keeping=`echo ${line} | awk '{print $5}'`

 #. ${n_profile}
 find ${n_log_dir} -iname ${n_log_wildcards} -mtime +${n_days_keeping} -type f | xargs rm -f
 fn_log "find ${n_log_dir} -iname ${n_log_wildcards} -mtime +${n_days_keeping} -type f | xargs rm -f"
done < ${n_inputf}
複製程式碼