
Linux学习总结(76)—— Shell 脚本日志技巧
发布日期:2025-04-08 23:16:31
浏览次数:11
分类:精选文章
本文共 483 字,大约阅读时间需要 1 分钟。
常见日志打印方式
编写 shell 脚本时,最简单的日志打印是使用 echo 输出,比如: 下面是打印一个测试程序的日志
[root@VM-0-2-centos ~]# echo "this is a log test..."this is a log test...
上面的方式使用起来非常简单,编写效率也很高,对于一些功能简单,代码量少的shell脚本,非常适合这种日志打印的方式,简单快捷
通用接口
上面的小节中,每次都用 echo ,没有一个统一的输出格式,如果需要在每条日志开头加上指定内容, 就需要修改脚本中每一处的日志,这就变成了一个体力活了
针对这点,可以定义一个输出函数,所有的的输出统一调用这个函数, 现有测试脚本 t.sh 内容如下
#!/bin/bashfunction log(){ echo "$@"}
另外新建一个测试脚本 ta.sh
#!/bin/bashsource ./t.shlog "this is a test..."log "today is `date '+%Y-%m-%d'` "
执行 ./ta.h 命令,结果如
发表评论
最新留言
不错!
[***.144.177.141]2025年04月24日 04时00分50秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux工作笔记026---Centos7.3 yum提示Another app is currently holding the yum lock; waiting for it to exit.
2023-02-04
Linux工作笔记027---Centos7.3 查看tomcat所占用的端口号
2023-02-04
Linux工作笔记028---Centos7.3 netstat命令详解
2023-02-04
Linux工作笔记031---Centos7.3下安装tomcat
2023-02-04
Linux工作笔记033---Linux(CentOS7)安装zip、unzip命令
2023-02-04
Linux工作笔记034---linux tail命令详解_linux在vi中查找字符串
2023-02-04