1、sh [-nvx] scripts
-n 不执行脚本,查询脚本内语法,若有错误则列出
-v 执行脚本前,先将脚本内容显示在屏幕
-x 将用到的脚本内容显示在屏幕上,与-v稍有不同
2、set [-nvx]
-n 读命令但不执行
-v 显示读取的行
-x 显示所有命令及参数
如下例子:
#!/bin/bash
#error
set -x
LIST="Peter Susan John Barry Lucy"
echo -n "Enter your name: "
read NAME
for LOOP in $LIST
do
if [ "$LOOP" = "$NAME" ]; then
echo "you're on the list, you're in"
break
fi
done
set +x
执行结果如下:
linux_NGIN:~/script/error_debug> ./error
+ LIST='Peter Susan John Barry Lucy'
+ echo -n 'Enter your name: '
Enter your name: + read NAME
aaa
+ for LOOP in '$LIST'
+ '[' Peter = aaa ']'
+ for LOOP in '$LIST'
+ '[' Susan = aaa ']'
+ for LOOP in '$LIST'
+ '[' John = aaa ']'
+ for LOOP in '$LIST'
+ '[' Barry = aaa ']'
+ for LOOP in '$LIST'
+ '[' Lucy = aaa ']'
+ set +x
3、在容易出错的地方多使用echo语句,且不要在echo后直接加最后状态命令,因为永远为真
4、set也可用于在脚本内部给出参数,某段脚本控制2个参数但不向脚本传递参数而在内部取值,如下
#!/bin/bash
#set_ex
set accounts.doc accouts.bak
while [ $# != 0 ]
do
echo $1
shift
done