0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

shell_シェルスクリプト_テキストで定義されたファイルを削除する

Posted at

Find log files and delete, depends on settings as setted by /huishoubao/config/clean_up_log.config.

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

dependency: logToFile.sh

#!/usr/bin/env bash
#log path is /var/log/yanke_shell_log.log
# Version:      1.1
# Update date:  20180320

if [ -d /var/log ]
then
    echo '/var/log/ exists...'
else
    mkdir -pv /var/log/
fi

# Default log file is /var/log/yanke_shell_log.`date +%F`
if [ `echo "${LOG_FILE}"| grep -v ^$| wc -l` -eq 0 ]; then
  LOG_FILE=/var/log/yanke_shell_log.log
fi

echo "Default LOG_FILE is ${LOG_FILE}"

function log_info () {
DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
USER_N=`whoami`
echo "${DATE_N} ${USER_N} execute $0 [INFO] $@" >> ${LOG_FILE} #执行成功日志打印路径
}

function log_error () {
DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
USER_N=`whoami`
echo -e "\a"
echo -e "\033[41;37m ${DATE_N} ${USER_N} execute $0 [ERROR] $@ \033[0m"  >> ${LOG_FILE} #执行失败日志打印路径
}

function fn_log () {
if [  $? -eq 0  ]
then
    log_info "$@ successed."
    echo -e "\033[32m $@ sucessed. \033[0m"
else
    log_error "$@ failed."
    echo -e "\033[41;37m $@ failed. \033[0m"
    exit 1
fi
}

# trap 'fn_log "DO NOT SEND CTR + C WHEN EXECUTE SCRIPT !!!! "'  2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?