在Linux上从Java执行grep命令时总是返回null
我正在linux文件上执行java中的grep命令。以下代码总是返回null
Process p;
String matchStr="testmatch";
String output = null;
try {
String command = "grep \""+matchStr+"\" "+ filename;
System.out.println("Running command: " + command);
p = Runtime.getRuntime().exec(command);
System.out.println("***********************************");
System.out.println("***********************************");
System.out.println("***********************************");
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (br.readLine() != null) {
System.out.println("in while loop");
System.out.println("in while loop");
System.out.println("in while loop");
System.out.println(output);
System.out.println("***********************************");
System.out.println("***********************************");
System.out.println("***********************************");
System.out.println("***********************************");
// Process your output here
}
System.out.println("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
若我直接grep它,它会显示输出,但从java它永远不会进入while循环。 请告诉我这里出了什么问题
# 1 楼答案
我最近也在努力解决一个类似的问题,我相信我找到的解决方案也能解决你的问题(尽管你的问题有点像其他人指出的那样格式不正确)
该问题与搜索字符串周围的引号有关
javaexec命令会直接将这些内容传递给grep命令,grep不会搜索matchStr,而是会搜索“matchStr”,结果将不是您所期望的
这也适用于以数组形式执行命令的情况,如
将普通搜索字符串传递到字符串中,但不包括引号
# 2 楼答案
把你的代码变成https://stackoverflow.com/help/mcve后,它对我有用
此处文件不存在:
现在文件确实存在,但不包含要查找的文本:
现在该文件已存在并包含以下文本:
以下是代码:
# 3 楼答案
问题是,您没有向
output
写入任何内容,因此它保持null
。我想你必须像这样重写你的while循环请注意,大多数样式检查都不支持这种语法,因为它的强大性
另外,最好在while循环之后放置
p.waitFor()
,这样grep就不会挂在flushigstd(err|out)
上更新
此外,最好使用^{} (自java-7起提供)而不是
Runtime.getRuntime().exec(...)
,因为这样可以更好地控制流程,即