问题描述
你最喜欢的方法是什么来处理Bash中的错误?在网页上找到的错误处理最好的例子是William Shotts,Jr在。他建议在Bash中使用以下函数进行错误处理:
##/ bin / bash
#一个slicker错误处理例程
#我在我的脚本中放了一个名为PROGNAME的变量,
#程序正在运行。您可以从命令行($ 0)的第一个项目获取
#的值。
#参考:这是从< http://www.linuxcommand.org/wss0150.php>
PROGNAME = $(basename $ 0)
函数error_exit
{
#----------- -------------------------------------------------- ---
#由于致命程序错误而退出的功能
#接受1个参数:
包含描述性错误消息的字符串
#---------- -------------------------------------------------- ----
echo$ {PROGNAME}:$ {1: - 未知错误}1& 2
退出1
}
#示例调用error_exit函数。注意包含
#的LINENO环境变量。它包含当前的
#行号。
echo行号和消息
error_exit的错误示例$ LINENO:发生错误。
您是否在Bash脚本中使用了更好的错误处理例程?
使用陷阱
tempfiles = )
cleanup(){
rm -f$ {tempfiles [@]}
}
陷阱清理0
错误(){
本地parent_lineno =$ 1
本地消息=$ 2
本地代码=$ {3:-1}
如果[[-n$ message]] ;然后
echo错误在$ {parent_lineno}上或附近:$ {message};退出状态$ {code}
else
echo错误在或附近$ {parent_lineno} ;退出状态$ {code}
fi
退出$ {code}
}
陷阱'错误$ {LINENO}'ERR
...然后,每当您创建一个临时文件:
temp_foo =$(mktemp -t foobar.XXXXXX)
tempfiles + =($ temp_foo)
和 $ temp_foo
将在退出时删除,并打印当前行号。 ( set -e
同样会给你错误的退出行为,并削弱了代码的可预测性和可移植性。)
您可以让陷阱调用错误
(在这种情况下,它使用默认退出代码1,没有消息)或自己调用并提供显式值;例如:
错误$ {LINENO}foobar failed2
/ pre>
将退出状态2,并给出明确的消息。
What is your favorite method to handle errors in Bash? The best example of handling errors I have found on the web was written by William Shotts, Jr at http://www.linuxcommand.org.
He suggests using the following function for error handling in Bash:
#!/bin/bash # A slicker error handling routine # I put a variable in my scripts named PROGNAME which # holds the name of the program being run. You can get this # value from the first item on the command line ($0). # Reference: This was copied from <http://www.linuxcommand.org/wss0150.php> PROGNAME=$(basename $0) function error_exit { # ---------------------------------------------------------------- # Function for exit due to fatal program error # Accepts 1 argument: # string containing descriptive error message # ---------------------------------------------------------------- echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2 exit 1 } # Example call of the error_exit function. Note the inclusion # of the LINENO environment variable. It contains the current # line number. echo "Example of error with line number and message" error_exit "$LINENO: An error has occurred."
Do you have a better error handling routine that you use in Bash scripts?
解决方案Use a trap!
tempfiles=( ) cleanup() { rm -f "${tempfiles[@]}" } trap cleanup 0 error() { local parent_lineno="$1" local message="$2" local code="${3:-1}" if [[ -n "$message" ]] ; then echo "Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}" else echo "Error on or near line ${parent_lineno}; exiting with status ${code}" fi exit "${code}" } trap 'error ${LINENO}' ERR
...then, whenever you create a temporary file:
temp_foo="$(mktemp -t foobar.XXXXXX)" tempfiles+=( "$temp_foo" )
and
$temp_foo
will be deleted on exit, and the current line number will be printed. (set -e
will likewise give you exit-on-error behavior, and weakens code's predictability and portability).You can either let the trap call
error
for you (in which case it uses the default exit code of 1 and no message) or call it yourself and provide explicit values; for instance:error ${LINENO} "the foobar failed" 2
will exit with status 2, and give an explicit message.
这篇关于在Bash中处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!