shell腳本的測試大家經常使用的方法就是使用echo出數據來看是否符合程序運算的值。
如果腳本過大的,這使用定義的常量開關開控制echo的輸出。
這裡給大家介紹一個方法,在腳本裡面#!/usr/bin/sh後設置一行set -xv就可以直接進入調試模式了。
而且一目了然。
請看如下列子:
[www.linuxidc.com @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
[www.linuxidc.com @linuxsource]$cat yu
#!/bin/sh
#[email protected]
#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
[www.linuxidc.com @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
[www.linuxidc.com @linuxsource]$vim yu
vim: /usr/lib/libxml2.so.2: no version information available (required by /usr/lib/libbonoboui-2.so.0)
加上:set -xv
[www.linuxidc.com @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
[www.linuxidc.com @linuxsource]$sh -xv yu sina
#!/bin/sh
#[email protected]
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參數也一樣可以調試。