如果命令失败如何退出? [复制] - How to exit if a command failed? [duplicate]

问题:

This question already has answers here : 这个问题在这里已经有了答案
Closed 6 months ago . 6 个月前关闭。

I am a noob in shell-scripting.我是 shell 脚本的菜鸟。 I want to print a message and exit my script if a command fails.如果命令失败,我想打印一条消息并退出我的脚本。 I've tried:我试过了:

my_command && (echo 'my_command failed; exit)

but it does not work.但它不起作用。 It keeps executing the instructions following this line in the script.它继续执行脚本中这一行后面的指令。 I'm using Ubuntu and bash.我正在使用 Ubuntu 和 bash。


解决方案:

参考一: https://en.stackoom.com/question/G2RB
参考二: https://stackoom.com/question/G2RB
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章