Sam*_*Sam 3 unix bash shell scripting
当我运行脚本时,出现此错误
awk: fatal: cannot open file `text.txt' for reading (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
问题是该脚本大约有450行,很难真正找到所要讨论的位置
如果我能得到像这样的行号那就太好了
awk: fatal: cannot open file `text.txt' for reading (No such file or directory) at line ***
Run Code Online (Sandbox Code Playgroud)
或者如果我设置-x然后
awk: fatal: cannot open file `text.txt' for reading (No such file or directory)
Terminated at line ***
Run Code Online (Sandbox Code Playgroud)
要将行号添加到bash -x输出:
PS4='$LINENO:' bash -x script
Run Code Online (Sandbox Code Playgroud)
对于脚本相互调用的复杂情况,不但要知道行号而且要知道文件名可能很方便:
PS4='$BASH_SOURCE:$LINENO:' bash -x script
Run Code Online (Sandbox Code Playgroud)
PS4可以根据需要进一步进行定制。例如:
PS4='File=$BASH_SOURCE: LineNo=$LINENO: ' bash -x script
Run Code Online (Sandbox Code Playgroud)
如果知道要查找的错误消息,则可以过滤输出以获取该消息及其相应的文件名和行号:
$ 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)
Run Code Online (Sandbox Code Playgroud)