注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

liangxh2008的博客

 
 
 

日志

 
 

How to debug a Shell Script under Linux or UNIX  

2010-05-12 15:57:56|  分类: shell |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

http://www.cyberciti.biz/tips/debugging-shell-script.html

Raju asks:

How can I Debug a shell scripts?

This is most common question asked by new admins or UNIX user.

Shell scripting debugging can be boring job (read as not easy). There are various ways to debug a shell script.

-x option to debug a shell script

Run a shell script with -x option.
$ bash -x script-name
$ bash -x domains.sh

Use of set builtin 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

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.

_ranger_ January 31, 2007 at 3:30 pm

For running under “set -x” or “set +x”, why not just run it as:

bash -x ./script.sh

Also, instead of having _DEBUG in your script, just export it from your shell:

$ export _DEBUG=on

Now, you have to make absolutely no changes to enable or disable all debugging.

Reply

2 nixcraft February 1, 2007 at 10:00 pm

_ranger_,

Good idea however you may have to modify script little to check if _DEBUG is defined or not.

Appreciate your post.

Reply


psc January 27, 2010 at 10:01 pm

[ "$_DEBUG" == "on" ] && $@ || :

Why are you using last || operator?

Reply

9 Vivek Gite January 28, 2010 at 4:36 am

Yes, it is not required.

Reply



  评论这张
 
阅读(766)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017