如何在Java中保存shell脚本的echo输出 [英] How to save a shell script's echo output within Java

查看:273
本文介绍了如何在Java中保存shell脚本的echo输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Java程序,它在JUnit测试中调用几个Shell脚本(因此除了。和E之外没有输出)操作文件和SQL表,然后将所述表中的数据与它们的期望值进行比较。一切都在理论上有效,但是我遇到了一个例子,一种类型的测试按预期工作,另一种类型在十分钟超时后失败。我已将问题隔离为Shell脚本之一,但是我没有编写此脚本,我无法以任何方式更改它。我可以解决此问题的最佳方法是保存相关脚本的输出,该输出来自一系列回显调用。有没有一种直接/可靠的方法来保存回声输出?我用谷歌搜索了一下,但没有任何结果。我还没有尝试存储输出,所以我没有试图向您展示,但我已经编写的可能相关的代码是:

I'm working on a Java program that calls several Shell scripts within JUnit tests (hence no output except .'s and E's) that manipulate files and SQL tables and then compares data in said tables to their expected values. Everything works in theory, however I'm encountering a strage bug where one type of test works as expected while the other type fails after a ten minute time-out. I've isolated the problem to be one of the Shell scripts, however I did not write this script and I'm unable to change it in any way. The best way I can troubleshoot this is saving the output of the script in question, which comes from a series of echo calls. Is there a somewhat straightforward/reliable way to save echo output? I've googled a bit but nothing's come up. I haven't tried to store the output yet so I don't have an attempt to show you, but the code I've already written that may be relevant is:

//Currently using BufferedWriter, File, FileWriter, IOException, and Writer
import java.io.*;

//Method to write to a log file
public void record(String input) throws IOException {
  writer = new BufferedWriter(new FileWriter(file,true));
  writer.write(input + "\n");
  writer.close();
}

Runtime rt = Runtime.getRuntime();

//Several of these since a lot of file manipulation/script running occurs
process = rt.exec("./shell.sh");
process.waitFor();

在我问的另一个问题中,有人建议不要使用Runtime.exec()有几个原因。虽然我意识到不使用exec()的优点,我需要为这个版本的程序。

In another question I asked, someone recommended not using Runtime.exec() for several reasons. While I realize the advantages to not using exec(), I need to for this version of the program.

tl; dr问题:我的Java程序可以轻松运行shell脚本以及保存echo的输出?如果没有,是否有一个尾部-f等效的读取输出?

The tl;dr question: Can my Java program easily run shell scripts as well as save their echo'd output? If not, is there a tail -f equivalent for reading the output?

推荐答案

为了尽可能轻松地做到这一点,你应该真的使用Asyncronous读写外部进程的输出。这可能是阻塞并造成超时的原因,就是脚本 echo 回放它的内容的方式,并且它们没有被消耗。

To do this as painlessly as possible you should really use Asyncronous reading and writing of the output of the external process. That is what is probably blocking and causing the time outs, is the way the script is echoing it's contents back, and they are not being consumed.

我刚刚发布了这个完全相同的问题的答案,这适用于所有从外部进程读取的情况,为什么为什么不再使用 Process.exec()并使用 ProcessBuilder

I posted an answer to this exact same question a while ago, this works in all situations when reading from external processes, and is the why of why not to use Process.exec() anymore and use ProcessBuilder.

这篇关于如何在Java中保存shell脚本的echo输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆