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 {1..6};
do
    set -x
    echo $i
    set +x
done
echo "script executed"

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

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

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

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

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

怕什么真理无穷,进一寸有一寸的欢喜。---胡适
原文地址:https://www.cnblogs.com/hujianglang/p/6978161.html