CentOS Shell Template Log Plus

 

说明:在模板的脚本中,加入根据配置文件对脚本变量的变更、功能文件对脚本函数的变更。该例子就是对脚本的Log功能进行重写。

文件:stcn_kuaixun.cnf 重置脚本的变量或引用skincare.fn中对应的功能。
文件:skincare.fn 对脚本的函数功能进行重写。
脚本:只负责实现功能逻辑。

执行效果:

[ root@stock.data.news.100 pts/1 2021-01-05/31@2 17:31:25 /Server/datas/downloads/scripts ] 
# bash spider.sh config_file=stcn_kuaixun.cnf skincare_file=skincare.fn   
2021-01-05 17:31:39 bash ./spider.sh process is 3748 
  current directory: /Server/datas/downloads/scripts
base function: receive parameters
  lock file = /tmp/spider.sh.lf
  config file = stcn_kuaixun.cnf
  skincare file = skincare.fn
base function: Initialize
  load config file . 
  Configuration[stcn_kuaixun.cnf] complete loading .
  Configuration[skincare.fn] complete loading .
  check script is already running .
  creating lock file .
  lock file is completed
Main 1 2 3 4
  5 6 7 8
  9
unlock file . 
  lock file is deleted
2021-01-05 17:31:39|finish 

 

无重写Log功能的效果:

[ root@stock.data.news.100 pts/1 2021-01-05/31@2 17:31:39 /Server/datas/downloads/scripts ] 
# bash spider.sh config_file=stcn_kuaixun.cnf 
2021-01-05 17:49:08 bash ./spider.sh process is 3765 
  current directory: /Server/datas/downloads/scripts
base function: receive parameters
  lock file = /tmp/spider.sh.lf
  config file = stcn_kuaixun.cnf
  skincare file = ./spider.fn
base function: Initialize
  load config file . 
  Configuration[stcn_kuaixun.cnf] complete loading .
  ./spider.fn does not exist .
  check script is already running .
  creating lock file .
  lock file is completed
Main 1 2 3 4 5 6 7 8 9
unlock file . 
  lock file is deleted
2021-01-05 17:49:08|finish

 

[ root@stock.data.news.100 pts/1 2021-01-05/31@2 17:27:44 /Server/datas/downloads/scripts ] 
# cat spider.sh    
#!/bin/bash

VERSION=2.0.1
Help()
{
    cat <<-EOF
    # synopsis: bash $0
    # description: 
    # date: 2020-11-03
    # version: ${VERSION} [ 增脚本传参,多进程,等各项改进 ]
    # author: LBC

    # -- 使用介绍
        #   单进程(默认在脚本所在的目录搜索${0%.*}.cnf、${0%.*}.fn;若没有配置则按脚本默认的方式执行。)
            # bash $0 lock_file=sample1.lf config_file=file1.cnf skincare_file=file1.fn
        
        #   参数项
            # lock_file 文本锁,用于区别不同进程
            # config_file 配置文件,用于修改脚本的全局变量
            # skincare_file 功能文件,用于重新脚本的函数功能
    
EOF
}

# 版本定义
# version: 2.0.0 [ 结构(主版本号,实现的逻辑).基础功能变更(新增或删除).基础功能优化 ]

# 命名约定
    # 全局变量:全大写+下划线
    # 函数:每个单词的首字母大写
    # 局部变量:小写+下划线

# 版本说明
    # 2.0.0 [ 基础版本 ]

# 全局变量初始化
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export PATH="/ProgramFiles/redis_slink/bin:/ProgramFiles/mysql_slink/bin:${PATH}"

WORKING_DIRECTORY=$(dirname $0)
LOCK_FILE="/tmp/$(basename $0).lf"
FILE_NAME=$(basename ${0%.*})
CONFIG_FILE="${WORKING_DIRECTORY}/${FILE_NAME}.cnf"
SKINCARE_FILE="${WORKING_DIRECTORY}/${FILE_NAME}.fn"
LOG_ON=1
LOCK_ON=1

# 日志输出
Log()
{
    if [ ${LOG_ON:-0} -eq 1 ] ; then
        echo "$1 $2"               
    fi
}

# [ 基础功能 ] 文件锁,防止脚本在执行期间重复执行. 
# $1=锁文件路径,$2=[0|1],0:检查锁文件是否存在,1:创建或删除锁文件
LockFile()
{
    if [ ${LOCK_ON:-0} -eq 0 ] ; then
        return 0
    fi
    local lock_file=$1
    if [ ${2:-0} -eq 0 ] ; then 
        if [ -e ${lock_file} ] ; then
            Log " " "error: $(stat -c %y ${lock_file}) script is already running."
            Log   "repair: Stop the process($(cat ${lock_file})) and delete the file(${lock_file})"
            exit
        fi
    else
        if [ -e ${lock_file} ] ; then
            rm -f ${lock_file}
            Log   lock file is deleted
        else
            echo $$ > ${lock_file}
            Log   "lock file is completed"
        fi
    fi
}

# [ 基础功能 ] 加载配置:$1=配置文件,$2=配置标签。
LoadConfigToRunning()
{
    local config_file=$1
    local config_label_list=$2
    local config_variables=‘‘
    local config_label=‘‘
    for config_label in "${config_label_list}"
    do
        config_variables=$(sed -rn /^\[${config_label}\]/,/^$|^}/{/^$|^#|^\[/d;p} ${config_file})
        eval "${config_variables}"
        
        #echo "${config_label_list}"
        #echo "${config_variables}"
    done
}

# [ 基础功能 ] 获取脚本配置文件(配置文件与脚本名同名) 
LoadConfigFile()
{
    local config_file=‘‘
    local config_labels=‘‘
    for config_file in $@
    do 
        if [ -e ${config_file} ] ; then
            dos2unix "${config_file}" &>/dev/null
            case ${config_file} in
                ${CONFIG_FILE})
                    config_labels=variables
                    ;;
                ${SKINCARE_FILE})
                    config_labels="$(sed -rn ‘/\[skincare\]/,/^$|^}/{/^$|^#|^\[/d;p}‘ ${CONFIG_FILE})"
                    ;;
            esac
            LoadConfigToRunning "${config_file}" "${config_labels:-‘‘}"
            Log   "Configuration[${config_file}] complete loading ."
        else
            Log   "${config_file} does not exist ."
            return 0
        fi
    done
}

# [ 基础功能 ] 接收脚本参数 
ReceiveParameters()
{
    Log "base function:" "receive parameters"
    #while [ "${1}" != "${1#*=}" ] ; do
    while [ 0 -ne $# ] ; do
        case ${1,,} in
            lock_file=?*)
                LOCK_FILE=${1#*=}                
                shift
                ;;
            config_file=?*)
                CONFIG_FILE=${1#*=}                
                shift
                ;;
            skincare_file=?*)
                SKINCARE_FILE=${1#*=}                
                shift
                ;;
            *)
                Help
                exit
                ;;
        esac
    done
    Log " " "lock file = ${LOCK_FILE}"
    Log " " "config file = ${CONFIG_FILE}"
    Log " " "skincare file = ${SKINCARE_FILE}"
}

# [ 基础功能 ] 初始化函数包含基础功能函数
Initialize()
{
    Log "base function:" "Initialize"
    Log   load config file . 
    LoadConfigFile ${CONFIG_FILE} ${SKINCARE_FILE}
    
    Log   check script is already running .
    LockFile ${LOCK_FILE}
    
    Log   creating lock file .
    LockFile ${LOCK_FILE} 1
}

Main()
{
    Log Main 1 2 3 4 5 6 7 8 9 4   # 将文本“1 2 3 4 5 6 7 8 9” 每行打印4个。

    Log unlock file .
    LockFile ${LOCK_FILE} 1
    Log "$(date +%F\ %H:%M:%S)|finish"
}

Log "$(date +%F\ %H:%M:%S) bash ${WORKING_DIRECTORY}/$(basename $0) process is $$"
Log   "current directory: $(pwd)" 

ReceiveParameters $@
Initialize
Main

 

[ root@stock.data.news.100 pts/1 2021-01-05/31@2 17:30:17 /Server/datas/downloads/scripts ] 
# cat stcn_kuaixun.cnf 
# 说明:标签与标签之间必须有空行,标签内的代码不能有空行,文件尾以空行为结束。
[variables]
LOG_ON=1
LOCK_ON=1
#

[skincare]
Log_2.1

 

[ root@stock.data.news.100 pts/1 2021-01-05/31@2 17:30:23 /Server/datas/downloads/scripts ] 
# cat skincare.fn 
# description: 功能替换。说明:标签与标签之间必须有空行,标签内的代码不能有空行,文件尾以空行为结束。
# date:
2020-11-07 # version: 2.0.0 # author: LBC # 命名约定 # 全局变量:全大写+下划线 # 函数:每个单词的首字母大写 # 局部变量:小写+下划线 # 版本号定义 # 支持脚本主版本号.替换/新增功能序号 # 对指定的网页上获取分页网址(折半查找获取) [GetWebPageUrls_2.1] GetWebPageUrls() { } # 日志输出格式化 [Log_2.1] # 参数:$1=顶格标题 # 参数:$2=内容 # 参数:$3=一行输出多个文本内容(单位:单词) Log() { if [ ${LOG_ON:-0} -eq 1 ] ; then local text_counter=0 local print_text=‘‘ # $1:一行的顶格单词(可以是序号,标题等) local text_title=$1 # $2:输出的文本内容 local text_list=$2 # $3:一行输出多少个文本内容(单位:单词),默认100个单词一行输出。 local text_merge_line=${3:-0} if [ ${text_merge_line} -eq 0 ] ; then echo "${text_title} ${text_list}" return 1 fi # print_text="${text_title}" for text in ${text_list:-" "} do if [ ${text_counter} -eq ${text_merge_line} ] ; then print_text="${print_text}\n ${text}" text_counter=0 else print_text="${print_text} ${text}" fi text_counter=$((${text_counter}+1)) done echo -e "${print_text}" fi }

 

CentOS Shell Template Log Plus

上一篇:JobRunShell.cs not FOUND


下一篇:使用sestatus命令来查看SELinux的当前状态