0

My application exits with error code 136 and I want to make sure it does.

For that I'm executing set +e to continue execution and then a condition:

> set +e
> java -jar myjar.jar
> if [ $? -eq 136 ]; then echo success; exit 0; else echo fail; exit 1; fi

This always ends up printing the following output:

fail

Does set +e modifies in any way the exit code? Why is it zero even when I'm exiting with 136?

I've also tried string comparison to no success:

EDIT:

I modified it according to the first answer and comments but the exit code still is not correct:

> java -jar myjar.jar
> EXIT_CODE=$?
> if [ $EXIT_CODE == 136 ]; then echo success; exit 0; else echo fail; exit 1; fi

I tried with different exit codes like 1 or 126 but it doesn't work.

I've also done the following:

> java -jar myjar.jar
> EXIT_CODE=$?
> echo $EXIT_CODE

and the result is 0 (it doesn't matter how I exit from my app)

The exit code in my app is the following:

        if (mycondition) {
            logger.log(LogLevel.WARN, "Exiting with error code 126");
            System.exit(126);
        } else {
            logger.log(LogLevel.WARN, "Exiting with error code 0");
            System.exit(0);
        }

and even when mycondition is true, the exit code in the shell is 0

Ivan
  • 57
  • 11
  • 26
  • 2
    `[ $? == 136 ]`, `echo success`, `echo fail`, etc are all commands, and each one replaces the exit status of the previous command. Also, using `&&` this way makes me nervous; what `&&` means is basically "execute the next command *only if* the last one succeeded. If you just want to execute a series of commands, separate them with `;` instead of `&&`. `echo` is unlikely to fail, but the implication of using `&&` is that you think it might fail, and if it does the next command should be skipped. – Gordon Davisson May 24 '22 at 06:31
  • 1
    As for your edit: what does `echo $EXIT_CODE` produce? Note that you're checking `$EXIT_CODE == 1`, not `$EXIT_CODE == 136` as in your first snippet. – Thomas May 24 '22 at 08:42
  • @Thomas it was a typo the echo produces a 0 (but I'm console logging inside the app and it's going through the code path where it can only exit with 1). – Ivan May 24 '22 at 09:02
  • No repro for the success case: https://ideone.com/mK8Bkb – tripleee May 24 '22 at 09:29
  • 2
    For the record: No, `set +e` does not modify any of the behavior around this; it simply undoes what `set -e` (exit on error) does. – tripleee May 24 '22 at 09:30
  • 1
    Your Java program's exit code is clearly 126 (one hundred and _twenty_ six) not 136. – tripleee May 24 '22 at 11:35

1 Answers1

1

$? echos 0 as the echo success command executes sucessfully, and therefore $? in echo $? is set to 0.
To echo the exit status from sh mycommand, either swap echo success && echo $? to echo $? && echo success or save the exit code of sh mycommand in a variable before the if-condition (and echo the variable).

Mime
  • 1,142
  • 1
  • 9
  • 20
  • I've changed it to the following and the EXIT_CODE is still 0 > java -jar mycommand.jar > EXIT_CODE=$? > echo $EXIT_CODE > if [ $EXIT_CODE == 1 ]; then echo success; exit 0; else echo fail; exit 1; fi – Ivan May 24 '22 at 08:47
  • 1
    Why do you compare to 1 if you wanted to make sure it's 136? – tripleee May 24 '22 at 08:57
  • @tripleee it was a typo – Ivan May 24 '22 at 09:02
  • Still, if your program really did exit with return code 136, that part of your original code should have worked. – tripleee May 24 '22 at 09:06
  • It seems like you are running java application and expected the `java` command to return e.g. `136`. To exit the java program with the code `136`, you must use `System.exit(136);` inside the java application. After that, `EXIT_CODE` should echo and equal `136`. [Related answer](https://stackoverflow.com/questions/6454114/java-exit-codes-and-meanings) – Mime May 24 '22 at 09:19
  • @Mime I'm exiting with System.exit(136); inside my app – Ivan May 24 '22 at 10:04