shell脚本里面一个很经典的调试方法

shell脚本的测试大家经常使用的方法就是使用echo出数据来看是否符合程序运算的值。
如果脚本过大的,这使用定义的常量开关开控制echo的输出。
这里给大家介绍一个方法,在脚本里面#!/usr/bin/sh后设置一行set -xv就可以直接进入调试模式了。
而且一目了然。
请看如下列子:
[ @linuxsource]$ls
ada  gcc-4.4.1  hotspot  httpd-2.2.19  httpd-2.2.19.tar.gz  leveldb-read-only  MySQL-5.1.7-beta  php-5.3.6  serial.tar.gz  yu  yuchao  yu.pl  yu.py  yu.rb
[ @linuxsource]$cat yu
#!/bin/sh
#yuchao1@staff.sina.com.cn
#set -xv
echo `ls  `


if [ $# != 1 ] ; then
echo "USAGE: $0 TABNAME"
echo " e.g.: $0 CDR_CALL_20040701"
exit 1;
fi


echo $0
echo $#


a="zzz"
if [ "$a" = "" ]
then
echo $a empty
else
echo $a no empty
fi
[ @linuxsource]$sh yu sina
ada gcc-4.4.1 hotspot httpd-2.2.19 httpd-2.2.19.tar.gz leveldb-read-only mysql-5.1.7-beta php-5.3.6 serial.tar.gz yu yuchao yu.pl yu.py yu.rb
yu
1
zzz no empty
[ @linuxsource]$vim yu
vim: /usr/lib/libxml2.so.2: no version information available (required by /usr/lib/libbonoboui-2.so.0)

加上:set  -xv

[ @linuxsource]$sh yu sina
echo `ls  `
+ ls
+ echo ada gcc-4.4.1 hotspot httpd-2.2.19 httpd-2.2.19.tar.gz leveldb-read-only mysql-5.1.7-beta php-5.3.6 serial.tar.gz yu yuchao yu.pl yu.py yu.rb
ada gcc-4.4.1 hotspot httpd-2.2.19 httpd-2.2.19.tar.gz leveldb-read-only mysql-5.1.7-beta php-5.3.6 serial.tar.gz yu yuchao yu.pl yu.py yu.rb


if [ $# != 1 ] ; then
echo "USAGE: $0 TABNAME"
echo " e.g.: $0 CDR_CALL_20040701"
exit 1;
fi
+ [ 1 != 1 ]


echo $0
+ echo yu
yu
echo $#
+ echo 1
1


a="zzz"
+ a=zzz
if [ "$a" = "" ]
then
echo $a empty
else
echo $a no empty
fi
+ [ zzz =  ]
+ echo zzz no empty
zzz no empty
[ @linuxsource]$sh -xv yu sina
#!/bin/sh
#yuchao1@staff.sina.com.cn
set -xv
+ set -xv
echo `ls  `
+ ls
+ echo ada gcc-4.4.1 hotspot httpd-2.2.19 httpd-2.2.19.tar.gz leveldb-read-only mysql-5.1.7-beta php-5.3.6 serial.tar.gz yu yuchao yu.pl yu.py yu.rb
ada gcc-4.4.1 hotspot httpd-2.2.19 httpd-2.2.19.tar.gz leveldb-read-only mysql-5.1.7-beta php-5.3.6 serial.tar.gz yu yuchao yu.pl yu.py yu.rb


if [ $# != 1 ] ; then
echo "USAGE: $0 TABNAME"
echo " e.g.: $0 CDR_CALL_20040701"
exit 1;
fi
+ [ 1 != 1 ]


echo $0
+ echo yu
yu
echo $#
+ echo 1
1


a="zzz"
+ a=zzz
if [ "$a" = "" ]
then
echo $a empty
else
echo $a no empty
fi
+ [ zzz =  ]
+ echo zzz no empty
zzz no empty

总结:
1,在程序脚本里面设置:set -xv
2.在命令行里面加上 -xv参数也一样可以调试。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/29dab0c39ccca562a1e1daa31c362604.html