如何正确调试bash脚本 [英] How to properly debug a bash script

查看:118
本文介绍了如何正确调试bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为了追查code正在使用bash脚本如下code

I am using the following code in a bash script in order to trace the code

#!/bin/bash
function trace()
{
    echo "TRACE" \
         "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:${FUNCNAME[1]}:" \
         "$BASH_COMMAND"
}

set -o functrace
shopt -s extdebug
trap trace DEBUG

# Remainder of code here
exit 0

但是,当我运行它,它吃我的变量。
一些线索?

But when I run it, it eats my variables. Some clue?

推荐答案

最好的办法是使用设置-xv 。在设置-v 将在执行之前回声线。该集 -x 将输出shell脚本插在该行的变量和前pressions后再行。

The best way is to use set -xv. The set -v will echo a line before it is executed. The set -x will output the line after the shell script interpolates the variables and expressions in that line.

作为其中的一部分,还可以创建一个名为 PS4 环境变量这将提示您的每一个shell脚本输出正在执行的线时间打印。大多数人把它设置成类似 PS =\\ $ LINENO:这将打印出的行号正在执行的行

As part of this, you can also create an environment variable called PS4 which will be the prompt printed each time your shell scripts outputs the line being executed. Most people set it to something like PS="\$LINENO: " which will print out the line number for the line being executed.

一旦你完成,你可以通过设置套装+ XV 关闭调试。

Once you're finished, you can turn off debugging by setting set +xv.

#
# Somewhere in this part of my script, I am having problems....
#
export PS4="\$LINENO> "   # Now, I'll see the line numbers while debugging
set -xv                   # Debugging is turned on
....
#
# Done with debugging
#
set +xv                   # Debugging is turned off

这篇关于如何正确调试bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆