How to exit if a command failed?

后端 未结 8 1493
猫巷女王i
猫巷女王i 2020-11-28 01:18

I am a noob in shell-scripting. I want to print a message and exit my script if a command fails. I\'ve tried:

my_command && (echo \'my_command failed         


        
8条回答
  •  误落风尘
    2020-11-28 01:46

    You can also use, if you want to preserve exit error status, and have a readable file with one command per line:

    my_command1 || exit $?
    my_command2 || exit $?
    

    This, however will not print any additional error message. But in some cases, the error will be printed by the failed command anyway.

提交回复
热议问题