bash - 在bash脚本: how to signal to the (human) user that an error triggered a premature exit?中

标签 bash error-handling

bash脚本中,如何通过某种自定义消息向(bash脚本调用人员)用户发信号通知已触发过早退出错误?
我想发送一个明确的失败消息,例如

! ** !

Oops, something went wrong.
This script did NOT successfully finish.

! ** !
以便用户了解脚本未成功完成。 (有时这一点还不清楚,并且用户错误地认为脚本已成功完成。)

最佳答案

回答我的评论,这里是扩展(Gilles Quenot's answer)如何在退出脚本时如何保留错误返回代码:

#!/usr/bin/env bash

trap 'rc=$?; cat<<EOF >&2
! ** !

Oops, something went wrong.
This script did NOT successfully finish.

! ** !
EOF
    exit $rc' ERR

failme ()
{
  return $1
}

failme 10

关于bash - 在bash脚本: how to signal to the (human) user that an error triggered a premature exit?中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62504165/

相关文章:

linux - 将多个参数传递给 bash

linux - 在非标准文件的模式匹配之前打印字符串

Bash elif 似乎没有被使用

python - 使用 default_error_handler 处理 Bottle/python 错误

codeigniter - 在CodeIgniter中实现数据库错误处理

php - Bash:递归查找php文件,用php预处理,捕获输出到html文件

bash - 如何在名称长度大于255个字节的youtube中下载视频?

java - 我对此文件输入/输出有错误。我有100个错误

bash - 使用调试退出主脚本的包装脚本

sql-server - 从TRY…CATCH block 调用时,SQL Server存储过程的返回码为NULL。