Shell编程常用函数
1、打印提示消息函数,不同级别消息使用不同的颜色显示。其中错误信息提示为红色字体。
# -------------------------------------------------------------------------------
# Function name: prompt_msg()
# Description : Pring prompt message to screen
# Usage : prompt_msg "INFO" "Hello World"
# -------------------------------------------------------------------------------
function prompt_msg()
{
[ $# -ne 2 ] && printf "\033[31mUsage: prompt_msg message_level message_info\n\033[0m"
local msg_level=$1
local msg_info=$2
[ ${msg_level} == "INFO" ] && printf "${msg_level}: ${msg_info}\n"
[ ${msg_level} == "WARN" ] && printf "\033[33m${msg_level}: ${msg_info}\n\033[0m"
[ ${msg_level} == "ERROR" ] && printf "\033[31m${msg_level}: ${msg_info}\n\033[0m"
}
2、写日志函数,将消息写入指定日志文件并在屏幕上显示。
# -------------------------------------------------------------------------------
# Function name: writelog()
# Description : Write script run log into log file
# -------------------------------------------------------------------------------
function writelog()
{
if [ $# -ne 3 ];then
echo "Usage: writelog message_level message_info logfile"
fi
local log_file=$1
local debug_level=$2
local messages=$3
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [${debug_level}] ${messages}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [${debug_level}] ${messages}" >> ${log_file}
}
3、替换函数,替换文件中指定的参数,匹配到指定的参数即整行替换,适用于文件中只有唯一参数的情况,可以通过匹配参数调整。
使用参数说明
searchStr:需要替换行的字符串。在开始行和结束行之间。这样可以精确匹配到该行。
replaceStr:替换匹配字符串所在的行。全行替换。脚本中替换代码为:sed -i "${strline}c ${replaceStr}" ${filename}
filename:待操作的文件。
# -------------------------------------------------------------------------------
## Funciton name : replaceLine
## Usage : replaceLine searchStr replaceStr filename
## Description : To replace the specify line
# -------------------------------------------------------------------------------
function replaceLine()
{
if [ $# -ne 3 ];then
prompt_msg "ERROR" "Usage:replaceLine searchStr replaceStr filename"
fi
local searchStr=$1
local replaceStr=$2
local filename=$3
if [ ! -f ${filename} ];then
prompt_msg "ERROR" "The file of ${filename} is not exist,please check."
fi
searchStr=$(echo ${searchStr} | sed -e 's/\[/\\[/g' -e 's/\]/\\]/g' -e 's/\./\\./g')
local strline=$(sed -n "/${searchStr}/=" ${filename})
if [ "X${strline}" != "X"];then
sed -i "${strline}c ${replaceStr}" ${filename}
return 0
else
prompt_msg "ERROR" "Replace parameter of ${searchStr} at ${filename} file failed."
return 1
fi
}
4、替换函数,替换文件中指定的参数,匹配到指定的参数即整行替换,适用于文件中有多个参数匹配的情况,通过增加行范围来实现。开始行和结束行通过指定模式匹配来获取。对比上一个函数的优点,就是可以指定区间修改,修改更精确。
使用参数说明:
startStr:输入开始匹配的参数,选择可唯一指定一行的字符串。目的是确定开始行。
endStr:输入结束匹配的参数。选择可唯一指定一行的字符串,目的是确定结束行。
searchStr:需要替换行的字符串。在开始行和结束行之间。这样可以精确匹配到该行。
replaceStr:替换匹配字符串所在的行。全行替换。脚本中替换代码为:sed -i "${strline}c ${replaceStr}" ${filename}
filename:待操作的文件。
# -------------------------------------------------------------------------------
##funciton: replaceLine2
##Usage: replaceLine2 startStr endStr searchStr replaceStr filename
##Description: To replace the specify line
# ------------------------------------------------------------------------------
function replaceLine2()
{
if [ $# -ne 5 ];then
prompt_msg "ERROR" "Usage:replaceLine2 startStr endStr searchStr replaceStr filename."
fi
local startStr=$1
local endStr=$2
local searchStr=$3
local replaceStr=$4
local filename=$5
if [ ! -f ${filename} ];then
prompt_msg "ERROR" "The file of ${filename} is not exist,please check."
fi
##transfer input startstr,endstr and searchstr
startStr=$(echo ${startStr} | sed -e 's/\[/\\[/g' -e 's/\]/\\]/g' -e 's/\./\\./g')
endStr=$(echo ${endStr} | sed -e 's/\[/\\[/g' -e 's/\]/\\]/g' -e 's/\./\\./g')
searchStr=$(echo ${searchStr} | sed -e 's/\[/\\[/g' -e 's/\]/\\]/g' -e 's/\./\\./g')
local startline=$(sed -n "/${startStr}/=" ${filename})
local endline=$(sed -n "/${endStr}/=" ${filename})
if [ "X${startline}" != "X" -a "X${endline}" != "X" ];then
local strline=$(sed -n "/${startline}/,/${endline}/{/${searchStr}/=}" ${filename})
if [ "X${strline}" != "X" ];then
sed -i "${strline}c ${replaceStr}" ${filename}
prompt_msg "INFO" "Replace parameter of ${searchStr} at ${filename} file success."
return 0
fi
fi
prompt_msg "ERROR" "Replace parameter of ${searchStr} at ${filename} file failed."
return 1
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义