我使用 set -e
停止执行第一个错误的脚本 。
问题是,这并没有告诉我出了什么问题。
如何更新一个bash 脚本, 从而显示我最后一个失败的命令?
我使用 set -e
停止执行第一个错误的脚本 。
问题是,这并没有告诉我出了什么问题。
如何更新一个bash 脚本, 从而显示我最后一个失败的命令?
而不是 set -e
,使用ERR陷阱;您可以通过 $BASH_LININNO
获得发生错误的具体行号。我在的回答中提供一个脚本,用这个脚本加以利用。
总结如下:
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
错误.sh
set -e
trap echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2 ERR
它包含在您所有脚本中 (. mistake.sh
) 。
替换任何
...\\\\在读 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
构造的一部分 ) 。
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)...
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
I like to write bash shell scripts on my iPhone, put them in an app folder with an icon, plist, etc., so they execute like apps by tapping their icon on SpringBoard. This is not interactive like ...
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 ...
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 ...
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 $(( ...
I m 试图撰写一个双面文字,处理一个文件清单,其名字在投入档案中每栏一栏储存,就像一个一样。
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 ...