在 Bash 脚本中,如果发生某种情况,如何退出整个脚本? [英] In a Bash script, how can I exit the entire script if a certain condition occurs?

查看:13
本文介绍了在 Bash 脚本中,如果发生某种情况,如何退出整个脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Bash 编写脚本来测试一些代码.但是,如果首先编译代码失败,则运行测试似乎很愚蠢,在这种情况下,我将中止测试.

I'm writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I'll just abort the tests.

有没有一种方法可以在不将整个脚本包装在 while 循环中并使用中断的情况下做到这一点?类似于 dun dun dun 转到?

Is there a way I can do this without wrapping the entire script inside of a while loop and using breaks? Something like a dun dun dun goto?

推荐答案

试试这个声明:

exit 1

用适当的错误代码替换 1.另请参阅具有特殊含义的退出代码.

Replace 1 with appropriate error codes. See also Exit Codes With Special Meanings.

这篇关于在 Bash 脚本中,如果发生某种情况,如何退出整个脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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