当我运行脚本时,我收到此错误
awk: fatal: cannot open file `text.txt' for reading (No such file or directory)
问题是脚本大约有450行,而且很难找到它所说的确切位置
如果我能得到像
这样的行号,那就太好了awk: fatal: cannot open file `text.txt' for reading (No such file or directory) at line ***
或者,如果我设置-x,那么只需
awk: fatal: cannot open file `text.txt' for reading (No such file or directory)
Terminated at line ***
答案 0 :(得分:5)
将行号添加到bash -x
输出:
PS4='$LINENO:' bash -x script
对于脚本相互调用的复杂情况,不仅可以知道行号而且还可以知道文件名:
PS4='$BASH_SOURCE:$LINENO:' bash -x script
PS4
可以根据需要进一步定制。例如:
PS4='File=$BASH_SOURCE: LineNo=$LINENO: ' bash -x script
如果我们知道我们要查找的错误消息,我们可以过滤输出以获取该消息及其相应的文件名和行号:
$ PS4='File=$BASH_SOURCE: LineNo=$LINENO: ' bash -x script 2>&1 | grep -B1 'awk: fatal:'
File=script: LineNo=3: awk 1 text.txt
awk: fatal: cannot open file `text.txt' for reading (No such file or directory)