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
}
相关文章
- Shell编程入门(第二版)(上)
- 【编程开发】x86,I386,i686, x86_64, x64,amd64、Windows Linux AIX下查看CPU位数和操作系统位数、rpm包名
- java ServerSocket服务端编程
- PLC编程顺控逻辑框架
- 10分钟入门Shell脚本编程
- 5个提高效率的编程工作环境
- [置顶] 学习JDK源码:编程习惯和设计模式
- Linux Shell脚本编程--Linux特殊符号大全
- Linux命令行与shell脚本编程大全(第3版)
- Linux 套接字编程中的 5 个隐患
- Linux&shell之高级Shell脚本编程-创建菜单
- Linux&shell之高级Shell脚本编程-创建函数
- linux shell脚本编程笔记(二): 分支结构
- 基于 OAuth 安全协议的 Java 应用编程
- linux笔记_day12_shell编程
- 2022-06-18青少年软件编程(C语言)等级考试试卷(四级)解析
- shell编程之while死循环
- 第43课:Spark 2.0编程实战之SparkSession、DataFrame、DataSet开发实战
- hadoop编程:分析CSDN注冊邮箱分布情况
- 混音器编程接口讨论
- linux shell 编程之运算符详解
- linux shell 编程之变量总结
- [C#] 走进异步编程的世界 - 开始接触 async/await
- AVR编程_如何通过软件复位AVR?(转)
- [Windows编程] 开发DLL必读《Best Practices for Creating DLLs》
- flutter入门之dart中的并发编程、异步和事件驱动详解
- linux c编程中调用 shell 命令
- Linux C 编程 | c程序调用shell脚本
- shell编程 | 输入成绩显示等级 A~E