Exit a Script On Error

后端 未结 5 739
温柔的废话
温柔的废话 2021-01-30 04:53

I\'m building a Shell Script that has a if function like this one:

if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias
then
            


        
5条回答
  •  被撕碎了的回忆
    2021-01-30 05:28

    If you put set -e in a script, the script will terminate as soon as any command inside it fails (i.e. as soon as any command returns a nonzero status). This doesn't let you write your own message, but often the failing command's own messages are enough.

    The advantage of this approach is that it's automatic: you don't run the risk of forgetting to deal with an error case.

    Commands whose status is tested by a conditional (such as if, && or ||) do not terminate the script (otherwise the conditional would be pointless). An idiom for the occasional command whose failure doesn't matter is command-that-may-fail || true. You can also turn set -e off for a part of the script with set +e.

提交回复
热议问题