提高bash脚本中的错误

编程入门 行业动态 更新时间:2024-10-27 08:33:12
本文介绍了提高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)。这里 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脚本中的错误

本文发布于:2023-10-09 10:26:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   错误   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!