简体   繁体   中英

set -e in Bash scripts: discovering failure location?

As we all know, we should use set -e to make Bash scripts more robust . However, with this enabled, if the script does fail, is there any way to get Bash to report which line of the script failed? At the moment, I'm having to re-run the script with a load of echo commands.

The best option I'm aware of for debugging with line numbers is to customise the " + " prefix printed by set -x to include $LINENO .

eg at the top of your script:

PS4='($LINENO)+ '
set -x

(Not exactly what you asked for, but it is handy!)

您可以使用bash -x重新运行(或者在脚本中set -x以激活调试, set +x以便稍后停用它)

不确定行号,但如果将-e(或-u)与-v组合使用,则会在屏幕上打印最后一行时出现错误发生的行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM