Redirect Java – version to a file or variable

This may be a stupid question, but I tried to redirect the exit of the "Java - version" command to a file or variable, but it didn't work

Server = Linux CentOS 6

My code in shell script

java -version >> test.txt

In addition, I'm trying to assign it to a variable:

JAVA_CHECK=`java -version`

Even if you run these commands from the command line, it still doesn't work properly

When I say it doesn't work, I mean that the exit of the command is displayed on my screen, not redirected to a file or anywhere

Solution

Java - version writes stderr (fileno 2) instead of stdout (fileno 1) You can redirect stderr to a file:

java -version 2> test.txt
# cat test.txt 
# java version "1.7.0_25"
# OpenJDK Runtime Environment
# [...]

Alternatively, you can redirect stderr to stdout:

java_check=$(java -version 2>&1)
# echo "$java_check"
# java version "1.7.0_25" OpenJDK Runtime Environment [...]
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>