欢迎 x1982129 加入本站!
 免费注册  用户登陆  汇款方式  汇款确认  产品报价  联系我们  帮助中心
加入收藏
设为首页
会员体系
申请VIP
网站首页 光盘超市 软件下载 技术文章 专题 用户中心 VIP会员 技术论坛 网站留言 娱乐中心 卓越资源
今天是:2008年11月20日 星期四  您现在位于: 首页 → 技术文章 → 如何在Unix/Linux...
   如何在Unix/Linux下调试脚本程序
作者:  出处:linux.chinaunix.net  更新时间: 2007年03月15日 

command
Bash shell offers debugging options which can be turn on or off using set command.
=> set -x : Display commands and their arguments as they are executed.
=> set -v : Display shell input lines as they are read.

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
#!/bin/bash
with (for debugging)
#!/bin/bash -xv

Method # 3: Use of intelligent DEBUG function
Add special variable _DEBUG. Set to `on’ when you need to debug a script:
_DEBUG="on"

Put the following function at the beginning of the script:

function DEBUG()
{
 [ "$_DEBUG" == "on" ] &&  $@ || :
}
Now wherever you need debugging simply use DEBUG function
DEBUG echo "File is $filename"
OR
DEBUG set -x
Cmd1
Cmd2
DEBUG set +x


When debugging done and before moving a script to production set _DEBUG to off
No need to delete debug lines.
_DEBUG="off" # set to anything but not to 'on'

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:
$ ./script.sh
Output:

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
_DEBUG="off"
Run script:
$ ./script.sh
Output:

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. 

 (本文已被浏览 2690 次)
 发布人:sdccf
 → 推荐给我的好友
上篇文章:彻底解决Fedora 6 中的添加删除与升级
下篇文章:使用Bash shell脚本进行功能测试
 相关文章:
一个awk应用的小例子 SCO的ksh环境下控制字符串输入的shell脚本
shell编程基础十二篇 Bash使用详解
shell中设置密码 执行加密脚本的实现
对话UNIX(6):通过脚本实现操作的自动化 bash 使用技巧
fish-用户友好的 Shell Colourshell:给 shell 命令着色
个性化你的shell提示符 运用提示行命令让你在Linux下玩魔术
Linux 技巧: Bash 测试和比较函数 SCO OpenServer下的系统管理shell
Oracle启动和关闭脚本For Linux freebsd中shell环境的设定
trap命令的用法 两个很详细的shell实例
Linux 技巧: Bash 测试和比较函数 shell运行九九乘法表

相关搜索
查看百度中关于如何在Unix/Linux下调试脚本程序的更多内容
查看google中关于如何在Unix/Linux下调试脚本程序的更多内容
   文章分类
操作系统 |
SCO_UNIX  Sun_Solaris  IBM_AIX  HP_UX  Linux  BSD  Tru64_UNIX 
通用UNIX知识  Windows  Minix 
程序设计 |
Shell编程  C/C++  汇编  PHP  JAVA  Perl  Python 
ASP/HTML  XML  中间件 
数据库 |
Oracle  Informix  Sybase  Fox  DB2  SQL  MySQL 
PostgreSQL 
网络应用 |
网络应用 
计算机硬件 |
计算机主机  打印机  路由器  交换机  终端  磁带机  MO 
刻录机  终端服务器  调制解调器 
   文章评论
  → 评论内容 (点击查看)   共0条评论,每页显示5条评论   浏览所有评论
(没有相关评论)
  → 发表我的评论
您的姓名: 您的Email:
评论内容:
250字内
发表评论:      发表评论须知 →
  • 尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法;
  • 本站有权保留或删除您发表的任何评论内容;
  • 关于我们 ┋  网站留言 ┋  网站地图 ┋  友情链接 ┋  与我在线 ┋  汇款确认 ┋  管理 ┋  TOP
    Linux.Unix爱好者家园  http://www.unix-cd.com/
    联系我们:sdccf@163.com
    腾讯QQ: 7644599
    备案序号:鲁ICP备05000455号
    Copyright (c) 2001-2008 Unix-cd.com. All Rights Reserved.