Linux 执行bash脚本时如何显示行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17804007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show line number when executing bash script
提问by dspjm
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.
我有一个测试脚本,它有很多命令并且会生成很多输出,我使用set -x
orset -v
和set -e
,所以脚本会在发生错误时停止。但是,我仍然很难找到执行停止的哪一行以定位问题。有没有一种方法可以在执行每一行之前输出脚本的行号?或者输出set -x
? 或者任何可以处理我的脚本行位置问题的方法都会有很大帮助。谢谢。
采纳答案by devnull
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.
您提到您已经在使用-x
. 变量PS4
表示该值是在-x
设置选项时回显命令行之前打印的提示,默认为:
后跟空格。
You can change PS4
to emit the LINENO
(The line number in the script or shell function currently executing).
您可以更改PS4
为发出LINENO
(当前正在执行的脚本或 shell 函数中的行号)。
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/debuggingtipsgives the ultimate PS4
that would output everything you will possibly need for tracing:
http://wiki.bash-hackers.org/scripting/debuggingtips给出了PS4
可以输出跟踪可能需要的所有内容的终极方法:
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
回答by hek2mgl
Simple (but powerful) solution: Place echo
around the code you think that causes the problem and move the echo
line by line until the messages does not appear anymore on screen - because the script has stop because of an error before.
简单(但功能强大)的解决方案:将echo
您认为会导致问题的代码放在周围并echo
逐行移动,直到消息不再出现在屏幕上 - 因为脚本之前因错误而停止。
Even more powerful solution: Install bashdb
the bash debugger and debug the script line by line
更强大的解决方案:安装bashdb
bash 调试器并逐行调试脚本
回答by Deqing
In Bash, $LINENO
contains the line number where the script currently executing.
在 Bash 中,$LINENO
包含脚本当前执行的行号。
If you need to know the line number where the function was called, try $BASH_LINENO
. Note that this variable is an array.
如果您需要知道调用函数的行号,请尝试$BASH_LINENO
. 请注意,此变量是一个数组。
For example:
例如:
#!/bin/bash
function log() {
echo "LINENO: ${LINENO}"
echo "BASH_LINENO: ${BASH_LINENO[*]}"
}
function foo() {
log "$@"
}
foo "$@"
See herefor details of Bash variables.
有关Bash 变量的详细信息,请参见此处。
回答by kklepper
Workaround for shells without LINENO
没有 LINENO 的 shell 的解决方法
In a fairly sophisticated script I wouldn't like to see all line numbers; rather I would like to be in control of the output.
在相当复杂的脚本中,我不想看到所有行号;相反,我想控制输出。
Define a function
定义一个函数
echo_line_no () {
grep -n "" echo_line_no "this is a simple comment with a line number"
| sed "s/echo_line_no//"
# grep the line(s) containing input with line numbers
# replace the function name with nothing
} # echo_line_no
Use it with quotes like
将它与引号一起使用
16 "this is a simple comment with a line number"
Output is
输出是
##代码##if the number of this line in the source file is 16.
如果源文件中这一行的编号是 16。
This basically answers the question How to show line number when executing bash scriptfor users of ash or other shells without LINENO
.
这基本上回答了问题如何在执行bash脚本时显示行数为灰的用户或其他炮弹没有LINENO
。
Anything more to add?
还有什么要补充的吗?
Sure. Why do you need this? How do you work with this? What can you do with this? Is this simple approach really sufficient or useful? Why do you want to tinker with this at all?
当然。你为什么需要这个?你如何处理这个问题?你能用这个做什么?这种简单的方法真的足够或有用吗?你为什么要修补这个呢?
Want to know more? Read reflections on debugging
想知道更多?阅读关于调试的思考