在Bash脚本中引发错误 [英] Raise error in a Bash script

查看:78
本文介绍了在Bash脚本中引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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).在这里,12564只是可以退出的随机代码.当您需要向OS指示程序异常停止(例如发生错误)时,您需要将非零退出代码传递给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 指出,您可以执行,这表示未指定的错误.

As @chepner pointed out, you can do exit 1, which will mean an unspecified error.

这篇关于在Bash脚本中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆