| ||
|
| Linux首页 | Linux论坛 | 论坛精华 | 开源新闻 | 技术文章 | 专题专栏 | 新手指南 | 迁移方案 | 产品方案 | 开源项目 | 开源图书 | 软件下载 | 人才招聘 | Linux博客 |
| 您的位置:
Linux时代 > 技术文档 > 程序开发 >
如何在Unix/Linux下调试脚本程序
commandBash shell offers debugging options which can be turn on or off using set command. You can use above two command in shell script itself: #!/bin/bash clear # turn on debug mode set -x for f in * do file $f done # turn OFF debug mode set +x ls # more commands You can replace standard Method # 3: Use of intelligent DEBUG functionAdd special variable _DEBUG. Set to `on’ when you need to debug a script: Put the following function at the beginning of the script: function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || :
}
Now wherever you need debugging simply use DEBUG function When debugging done and before moving a script to production set _DEBUG to off Sample script: #!/bin/bash
_DEBUG="on"
function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || :
}
DEBUG echo 'Reading files'
for i in *
do
grep 'something' $i > /dev/null
[ $? -eq 0 ] && echo "Found in $i file" || :
done
DEBUG set -x
a=2
b=3
c=$(( $a + $b ))
DEBUG set +x
echo "$a + $b = $c"
Save and run the script: Reading files Found in xyz.txt file + a=2 + b=3 + c=5 + DEBUG set +x + '[' on == on ']' + set +x 2 + 3 = 5 Now set DEBUG to off Found in xyz.txt file 2 + 3 = 5 Above is a simple but quite effective technique. You can also try to use DEBUG as an alias instead of function. 本文被浏览次
| |||||||||||
| 关于我们 | 联系方式 | 广告合作 | 诚聘英才 | 网站地图 | 免费注册 |
Copyright © 2001-2006 ChinaUnix.net All Rights Reserved 感谢所有关心和支持过ChinaUnix的朋友们 |