How to exit if a command failed?

后端 未结 8 1471
猫巷女王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 02:00

    If you want that behavior for all commands in your script, just add

      set -e 
      set -o pipefail
    

    at the beginning of the script. This pair of options tell the bash interpreter to exit whenever a command returns with a non-zero exit code.

    This does not allow you to print an exit message, though.

提交回复
热议问题