shell指令碼裡面一個很經典的調試方法
作者 餘超 email:yuchao86@gmail.com
shell指令碼的測試大家經常使用的方法就是使用echo出資料來看是否符合程式運算的值。
如果指令碼過大的,這使用定義的常量開關開控制echo的輸出。
這裡給大家介紹一個方法,
在指令碼裡面#!/usr/bin/sh後設定一行set -xv就可以直接進入偵錯模式了。
而且一目瞭然。
請看如下列子:
[yuchao@yuchao-Latitude-E5410 source]$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
[yuchao@yuchao-Latitude-E5410 source]$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
[yuchao@yuchao-Latitude-E5410 source]$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
[yuchao@yuchao-Latitude-E5410 source]$vim yu
vim: /usr/lib/libxml2.so.2: no version information available (required by /usr/lib/libbonoboui-2.so.0)
[yuchao@yuchao-Latitude-E5410 source]$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
[yuchao@yuchao-Latitude-E5410 source]$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參數也一樣可以調試。