English 中文(简体)
如何显示在使用 bash set -e 时失败的最后命令?
原标题:How to display last command that failed when using bash set -e?
  • 时间:2012-05-24 18:11:21
  •  标签:
  • bash

我使用 set -e 停止执行第一个错误的脚本 。

问题是,这并没有告诉我出了什么问题。

如何更新一个bash 脚本, 从而显示我最后一个失败的命令?

最佳回答
问题回答
  1. 错误.sh

    set -e
    trap  echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2  ERR
    
  2. 它包含在您所有脚本中 (. mistake.sh ) 。

  3. 替换任何

    ...\\\\在读 X; do...; 完成

    读取 X; do; do.; 完成 < < (...)

    在错误消息中给出正确行号/ 命令的陷阱脚本中

您是否尝试过 -verbose 吗?

bash --verbose script.sh

您不能自行使用 set - e , 因为处理会在任何错误发生后立即停止 。 查看 < a href=> http:// www. gnu. org/ software/ bash/ manual/ bashref. html# The- Set- Builtin" rel=“ nofollow” >Set Builtin 区域 , 了解更多关于 - x 和 - v 选项的信息, 您可以使用这些选项进行调试 。

类似 :

set -e
set -v

会在任何错误时退出, 同时显示您正在读取的每一输入行。 但是, 它不会显示您与错误的直线 。 为此, 您需要自己进行明确的错误检查 。

例如:

set +e
if false; then
    real_exit_status=$?
    echo  Some useful error message.  >&2
    exit $real_exit_status
fi

set -ex 将显示执行时的行(所有), 并在第一个命令返回非零时停止( 不是作为 if/ while/ to 构造的一部分 ) 。





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签