Exit a Script On Error

后端 未结 5 736
温柔的废话
温柔的废话 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:30

    Here is the way to do it:

    #!/bin/sh
    
    abort()
    {
        echo >&2 '
    ***************
    *** ABORTED ***
    ***************
    '
        echo "An error occurred. Exiting..." >&2
        exit 1
    }
    
    trap 'abort' 0
    
    set -e
    
    # Add your script below....
    # If an error occurs, the abort() function will be called.
    #----------------------------------------------------------
    # ===> Your script goes here
    # Done!
    trap : 0
    
    echo >&2 '
    ************
    *** DONE *** 
    ************
    '
    

提交回复
热议问题