Exit a Script On Error

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

    If you want to be able to handle an error instead of blindly exiting, instead of using set -e, use a trap on the ERR pseudo signal.

    #!/bin/bash
    f () {
        errorCode=$? # save the exit code as the first thing done in the trap function
        echo "error $errorCode"
        echo "the command executing at the time of the error was"
        echo "$BASH_COMMAND"
        echo "on line ${BASH_LINENO[0]}"
        # do some error handling, cleanup, logging, notification
        # $BASH_COMMAND contains the command that was being executed at the time of the trap
        # ${BASH_LINENO[0]} contains the line number in the script of that command
        # exit the script or return to try again, etc.
        exit $errorCode  # or use some other value or do return instead
    }
    trap f ERR
    # do some stuff
    false # returns 1 so it triggers the trap
    # maybe do some other stuff
    

    Other traps can be set to handle other signals, including the usual Unix signals plus the other Bash pseudo signals RETURN and DEBUG.

    0 讨论(0)
  • 2021-01-30 05:15

    exit 1 is all you need. The 1 is a return code, so you can change it if you want, say, 1 to mean a successful run and -1 to mean a failure or something like that.

    0 讨论(0)
  • 2021-01-30 05:20

    Are you looking for exit?

    This is the best bash guide around. http://tldp.org/LDP/abs/html/

    In context:

    if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias
    then
        echo $jar_file signed sucessfully
    else
        echo ERROR: Failed to sign $jar_file. Please recheck the variables 1>&2
        exit 1 # terminate and indicate error
    fi
    
    ...
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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 *** 
    ************
    '
    
    0 讨论(0)
提交回复
热议问题