Redirect java -version to file or variable

≯℡__Kan透↙ 提交于 2019-12-01 16:05:17

问题


Maybe it is a silly question, but I'm trying to redirect the exit of "java -version" command to a file or variable but it doesn't work.

Server = Linux CentOS 6

My code in shell script

java -version >> test.txt

Also I'm trying to assign it to a variable:

JAVA_CHECK=`java -version`

Even running those command from command-line it still not working.

when I say it doesnt work, I mean that the exit of the command is being showed in my screen instead to redirect it to a file or wherever

...


回答1:


java -version writes to stderr (fileno 2), not 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
# [...]

Or you can redirect stderr to stdout:

java_check=$(java -version 2>&1)
# echo "$java_check"
# java version "1.7.0_25" OpenJDK Runtime Environment [...]


来源:https://stackoverflow.com/questions/21453774/redirect-java-version-to-file-or-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!