bashexitshell

Exiting a script upon encountering an error


I'm building a shell script that has an if statement like this one:

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
fi

...

I want the execution of the script to finish after displaying the error message. How I can do this?


Solution

  • 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
    
    ...