bash - 使用 bash set -e 时如何显示最后一个失败的命令?

标签 bash

我正在使用 set -e 在第一次出错时停止执行脚本。

问题是这并没有告诉我哪里出了问题。

如何更新 bash 脚本,以便它显示上次失败的命令?

最佳答案

使用 ERR 陷阱代替 set -e;你可以传递$BASH_LINENO 来获取发生错误的具体行号。我在 https://stackoverflow.com/a/185900/14122 的回答中提供了一个利用此功能的脚本。

总结:

error() {
   local sourcefile=$1
   local lineno=$2
   # ...logic for reporting an error at line $lineno
   #    of file $sourcefile goes here...
}
trap 'error "${BASH_SOURCE}" "${LINENO}"' ERR

关于bash - 使用 bash set -e 时如何显示最后一个失败的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10743026/

相关文章:

bash - 粘贴 tee 命令的结果

regex - bash 中的正则表达式不贪婪

arrays - bash 函数返回更改后的索引

linux - 查找与某个 'count of commits' 匹配的所有 git 提交

linux - Lubuntu 中的基本菜单 BASH 脚本

linux - 为什么在gdb环境下不能使用键空间?

bash - Windows 10 的 Linux 子系统中的 apt 失败并显示 404 Not Found

node.js - node中的error,stderr,stdout有什么区别

linux - 使用 bash 脚本将两个最近的文件复制到另一个目录

bash - 在集群范围的初始化脚本中从 dbfs 复制文件