如何执行bash脚本时

如何执行bash脚本时

本文介绍了如何执行bash脚本时,显示行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我里面有很多命令的测试脚本,并会产生大量的输出,我用设置-x 设置-v 和设置-e ,所以当发生错误的脚本将停止。然而,它仍然是相当困难的,我找到这行做了停止执行,以查找问题。
有可输出执行每个行之前的脚本的行号的方法?
或输出由生成的命令展前行数设置-x
或者可以处理我的脚本线位置问题的任何方法将是一个很大的帮助。
谢谢你。

I have a test script which has a lot of commands and will generate lots of output, I use set -x or set -v and set -e, so the script would stop when error occurs. However, it's still rather difficult for me to locate which line did the execution stop in order to locate the problem.Is there a method which can output the line number of the script before each line is executed?Or output the line number before the command exhibition generated by set -x?Or any method which can deal with my script line location problem would be a great help.Thanks.

推荐答案

您提到,您已经在使用 -x 。变量 PS4 表示值是命令行前打印的提示时, -x 选项设置是呼应和默认为和空格

You mention that you're already using -x. The variable PS4 denotes the value is the prompt printed before the command line is echoed when the -x option is set and defaults to : followed by space.

您可以修改 PS4 来发出 LINENO (在脚本或shell函数当前正在执行的行号)

You can change PS4 to emit the LINENO (The line number in the script or shell function currently executing).

例如,如果你的脚本内容:

For example, if your script reads:

$ cat script
foo=10
echo ${foo}
echo $((2 + 2))

执行它因此将打印行数:

Executing it thus would print line numbers:

$ PS4='Line ${LINENO}: ' bash -x script
Line 1: foo=10
Line 2: echo 10
10
Line 3: echo 4
4

http://wiki.bash-hackers.org/scripting/debuggingtips给出了最终的 PS4 这将输出一切您可能需要追踪:

http://wiki.bash-hackers.org/scripting/debuggingtips gives the ultimate PS4 that would output everything you will possibly need for tracing:

export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

这篇关于如何执行bash脚本时,显示行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:13