shell脚本调试 -- 运行日志2
来源:互联网 发布:mac修图 编辑:程序博客网 时间:2024/06/04 00:41
shell脚本调试 -- 运行日志1
本文主要描述如何输出shell脚本中的debug日志.
C/C++的debug日志
在C/C++中有 __FILE__, __func__, __LINE__ 来表示 当前这行日志来自哪个源代码文件的第几行的哪个函数.
如:
#include<stdio.h>
/* t.c */
int main()
{
printf("%s:%d:%s:DEBUG Hello World!\n", __FILE__, __LINE__, __func__);
return 0;
}
/* 运行结果如下:
$> make t
cc t.c -o t
$> ./t
t.c:6:main:DEBUG Hello World!
$>
*/
t.c:6:main:DEBUG Hello World! 是在说, 这行信息来至 t.c 文件的第 6 行的 main 函数,这种方法可以让你在调试过程中很快的定位问题的位置.
shell脚本中的debug日志
使用 alias 功能, 实现类似C/C++中的debug日志方法. 将文件保存到 /etc/mydebug
#!/bin/bash
# mydebug
# Aliases are not expanded when the shell is not interactive, unless the
#+ expand_aliases shell option is set using shopt
shopt -s expand_aliases
case "$1" in
"debug")
alias mydebug='echo -n $(caller 0|tr " " ":"):$FUNCNAME:$LINENO:LOG:" "';
;;
*)
alias mydebug='';
;;
esac
# mydebug
# Aliases are not expanded when the shell is not interactive, unless the
#+ expand_aliases shell option is set using shopt
shopt -s expand_aliases
case "$1" in
"debug")
alias mydebug='echo -n $(caller 0|tr " " ":"):$FUNCNAME:$LINENO:LOG:" "';
;;
*)
alias mydebug='';
;;
esac
测试:
#!/bin/bash
# mydebug_test.sh
source /etc/mydebug debug; #如果带参数就是 debug 模式, 不带任何参数为非debug模式.
function check_apache()
{
echo "$(mydebug)apache [OK]";
}
function check_mysql()
{
echo "$(mydebug)mysql [OK]";
}
function check_all()
{
check_apache;
check_mysql;
}
check_all;
debug模式的运行结果:
$> /bin/bash mydebug_test.sh
18:check_all:mydebug_test.sh:check_apache:8:LOG: apache [OK]
19:check_all:mydebug_test.sh:check_mysql:13:LOG: mysql [OK]$>
非debug模式的运行结果:
$> /bin/bash mydebug_test.sh
apache [OK]
mysql [OK]From: GS
-------------------------------
- shell脚本调试 -- 运行日志2
- shell脚本调试 -- 运行日志1
- shell 脚本运行日志通用模块
- shell 编写jar运行脚本并制定日志输入文件
- 日志压缩shell脚本
- shell脚本学习日志
- shell脚本调试技巧
- Shell脚本调试技术
- Shell脚本调试技术
- Shell脚本调试技术
- Shell脚本调试技术
- Shell脚本调试技术
- Shell脚本调试技术
- shell脚本调试技术
- Shell脚本调试技术
- Shell脚本调试技术
- bashdb调试shell脚本
- 如何调试shell脚本
- JOS实验——Exercise 8
- CSS3+PNG实现GIF动画效果
- 通过案例深入探讨PHP中的内存管理问题
- windows7下回环网卡的安装
- android wifi ralink rt3070开发记录
- shell脚本调试 -- 运行日志2
- <筆記>Nullable Types
- 还是h264的熵编码,还是转的
- 二项堆的操作
- win8 & metro style app 学习资源
- IIC AT23C64芯片调试
- QT读取文件路径用C语言File打开
- HDOJ--2059 龟兔赛跑
- 领域特定语言