0

Currently we have a shell script that executes a Java class and redirects the output to a single file using 2>&1. I want to trap the output in the Unix shell after the Java class execution and find if there are any errors. Can this be done without checking the output file?

2 Answers 2

1

One option is to send the error output to a separate file from the standard output; then you only have to look and see whether the error file is empty or not. If it's empty, no errors were reported to stderr (which, sadly, does not guarantee that errors were not reported to stdout, but that's a QoI (quality of implementation) issue for the Java program).

You might also look at the exit status of the Java program. It will likely exit with a zero status on success and may exit with a non-zero status on failure. However, it might report errors to stderr and still exit successfully.

Depending on what the Java process does, you might be able to run it with the output captured in a variable by the shell using $(...) or backticks. You'd then scan the variable (instead of a file) to find out whether anything went wrong.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use tee:

myscript.sh 2>&1 | tee logfile

This will print all the output of your script (both stdout and stderr) to stdout and also to the logfile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.