Redirect java -version to file or variable(将 java -version 重定向到文件或变量)
问题描述
也许这是一个愚蠢的问题,但我试图将java -version"命令的退出重定向到文件或变量,但它不起作用.
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.
服务器 = Linux CentOS 6
Server = Linux CentOS 6
我在 shell 脚本中的代码
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
...
推荐答案
java -version
写入 stderr (fileno 2),而不是 stdout (fileno 1).您可以将 stderr 重定向到文件:
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 [...]
这篇关于将 java -version 重定向到文件或变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 java -version 重定向到文件或变量
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01