#在每一行前加上行号 export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: ' #进行调试 sh -x test.sh #调试结果 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 1 1 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 2 2 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 3 3 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 4 4 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 5 5 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 6 6 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 7 7 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 8 8 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 9 9 +test.sh:3:: for i in '{1..10}' +test.sh:5:: echo 10 10
有时候,你只需要对脚本的一部分进行调试,那么可以使用如下命令:
1 2 3 4
set -x #在执行时显示参数和命令 set +x #禁止调试 set -v #当命令行读取时显示输入 set +v #禁止打印输入
for i in {1..5} do DEBUG echo -e "This is debug line!" echo $i done
我们将_DEBUG环境变量设定为一个开关,只有打开时才会输出调试日志. 使用如上脚本结果如下:
1 2 3 4 5 6 7 8 9 10 11
[aidu1602@ResU10 tools]$ _DEBUG=on sh debug.sh This is debug line! 1 This is debug line! 2 This is debug line! 3 This is debug line! 4 This is debug line! 5