如果命令失敗如何退出? [複製] - 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章