Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm doing some audit automation, this example describes checking the version of Java, even though other programs do the same thing.

The output of "java -version" goes to STDERR, which is easily redirected, but I want to send shell errors (for example, when the java binary is missing) to /dev/null. It seems that shell redirection is an all-or-nothing proposition.

My most promising attempt so far as been:

{ /bin/ksh "{java -version 2>&1;}"; } 2>/dev/null

...which properly sends the output of the -version command to STDOUT, but if java isn't there, it sends the shell "not found" error to STDOUT as well. I don't want to see that message.

Same behavior with:

{ /bin/ksh "{java -version 2>&1;}" 2>/dev/null; }

Does anyone know a way to limit the scope of redirection so each process gets its own? I'm not limited to ksh, but for environment reasons it's got to be a shell based one-liner.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Is it not possible to check if java is found and executable?

test -x "$(which java)" && java -version 2>&1

or if you want to get around test and check for java-errors, too

java -version 2>/dev/null 1>/dev/null && java -version 2>&1
share|improve this answer
    
Using "test" is a different, promising approach. That could do the trick. –  GraduateOfAcmeU Jun 13 '12 at 19:12

You can redirect to additional descriptors. This is for bash, I think it works for ksh too:

exec 3>javalog
java ... 2>&1 >&3
exec 3>&-
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.