shell 函数模板

常用模板

日志打印

//函数
log(){
    local log_level=$1
    local log_info=$2
    local line=$3
    local script_name=$(basename $0)
    case ${log_level} in
    "info")
        echo -e "\033[32m$(date "+%Y-%m-%d %T.%N") [INFO]: ${log_info}\033[0m";;
    "warn")
        echo -e "\033[33m$(date "+%Y-%m-%d %T.%N") [WARN]: ${log_info}\033[0m";;
    "error")
        echo -e "\033[31m$(date "+%Y-%m-%d %T.%N") [ERROR ${script_name} ${FUNCNAME[1]}:$line]: ${log_info}\033[0m";;
    *)
        echo -e "${@}"
        ;;
    esac
    }
//用法
log info 'info日志'
log warn 'warn日志'
log error 'error日志'

替换Value

RECOVERVALUE() {
    local filename=$1
    local oldvalue=$2
    local newvalue=$3
    sed -i "s/${oldvalue}/${newvalue}/g"  ${filename}
}

//用法
RECOVERVALUE hello.txt gaolili liligao  

获取Value

GETVALUE() {
   local key=$1
   cat ${EXPORTERCONF}  | grep ${key} | awk -F= '{print $2}'
}
//用法
GETVALUE  name
posted @ 2022-07-11 11:09  liwenchao1995  阅读(12)  评论(0编辑  收藏  举报