在 Bash 脚本中引发错误 [英] Raise error in a Bash script
问题描述
我想在带有消息测试用例失败!!!"的 Bash 脚本中引发错误.如何在 Bash 中执行此操作?
I want to raise an error in a Bash script with message "Test cases Failed !!!". How to do this in Bash?
例如:
if [ condition ]; then
raise error "Test cases failed !!!"
fi
推荐答案
这取决于您想要存储错误消息的位置.
This depends on where you want the error message be stored.
您可以执行以下操作:
echo "Error!" > logfile.log
exit 125
或以下:
echo "Error!" 1>&2
exit 64
当您引发异常时,您将停止程序的执行.
When you raise an exception you stop the program's execution.
您还可以使用类似 exit xxx
的内容,其中 xxx
是您可能想要返回给操作系统的错误代码(从 0 到 255).这里 125
和 64
只是你可以退出的随机代码.当您需要向操作系统表明程序异常停止(例如发生错误)时,您需要将一个非零退出代码传递给exit
.
You can also use something like exit xxx
where xxx
is the error code you may want to return to the operating system (from 0 to 255). Here 125
and 64
are just random codes you can exit with. When you need to indicate to the OS that the program stopped abnormally (eg. an error occurred), you need to pass a non-zero exit code to exit
.
正如@chepner 指出,你可以做exit 1
,这意味着一个未指明的错误.
As @chepner pointed out, you can do exit 1
, which will mean an unspecified error.
这篇关于在 Bash 脚本中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!