ka3ak
ka3ak

Reputation: 3191

jshell always exits with zero status code

I use Linux and have noticed that jshell always exits with code 0 even if the java expression is invalid:

$ echo "invalid java expression" | jshell -
Error:
';' expected 
invalid java expression;
            ^
$ echo $?
0

Is it possible to force it to exit with a non-zero code somehow? I've read the help but have found nothing about it.

Upvotes: 1

Views: 138

Answers (2)

ka3ak
ka3ak

Reputation: 3191

The following solution doesn't necessarily answer the question but can be used as a workaround. It is based on redirecting standard and error output of jshell to corresponding files and evaluate them after the execution.

Create temporary files:

$ stdout=$(mktemp); stderr=$(mktemp)

Unsuccessful execution:

$ echo "invalid java expression" | jshell - 1>$stdout 2>$stderr

Evaluate the results:

$ cat $stdout 
$ cat $stderr 
Error:
';' expected
invalid java expression;
            ^

Successful execution:

$ echo "System.out.println(\"valid java expression\")" | jshell - 1>$stdout 2>$stderr
$ cat $stdout 
valid java expression
$ cat $stderr 
$

Upvotes: 0

Artur Radiuk
Artur Radiuk

Reputation: 44

Probably the easiest way is to run command '/exit' with proper argument.

echo "/exit -1" | jshell

Upvotes: 1

Related Questions