How to Run jar in Sequence inside Shell Script based on result?

Viewed 110

Suppose I am 2 jar file and need to run them from shell script, i can use the below commands in the same script.

java -jar myApp1.jar

java -jar myApp2.jar

So it will start myApp1.jar first and then myApp2.jar file.

Question: I want to run myApp2.jar only if the run of myApp1.jar is successful/completed. If myApp1.jar run is failed it will exit the shell script.

Can any one please help me how can I achieve that?

1 Answers
java -jar myApp1.jar || exit 1 && java -jar myApp2.jar

Or

#!/bin/sh

if java -jar myApp1.jar; then
    echo 'myApp1.jar -- Successful!'
    if java -jar myApp2.jar; then
        echo 'myApp2.jar -- Successful!'
    else
        echo 'myApp2.jar -- Failed!'
        exit 1
   fi
else
    echo 'myApp1.jar -- Failed!'
    exit 1
fi
Related