shell脚本使用技巧3--调试

时间:2023-03-09 18:58:10
shell脚本使用技巧3--调试

1.使用-x,开启shell脚本的跟踪调试功能

ex:bash -x script.sh

or

sh -x script.sh

2.使用set -x 和 set +x对脚本进行部分调试(输入中间的内容)

#!/bin/bash
#filename:debug.sh
for i in {..};
do
set -x
echo $i
set +x
done
echo "script executed"

3.固定格式生成调试信息

注:符号:告诉shell不要进行任何操作

#!/bin/bash
function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || : }
for i in {..}
do
DEBUG echo $i
done

执行命令:_DEBUG=on ./script.sh

4.修改脚本开头#!/bin/bash 为#!/bin/bash -xv自动开启调试功能;