我得到null作为此命令行的输出。
Process result = Runtime.getRuntime().exec(new String[]{"/usr/bin/find",baseDir+"/..","-type","f","|","/usr/bin/grep",filter1,"|","/usr/bin/grep",filter2,"|","/usr/bin/wc","-l"});
result.waitFor();
BufferedReader echo = new BufferedReader(new InputStreamReader(result.getInputStream()));
writer.print(echo.readLine());
echo.close();
是管道“|”吗?
答案 0 :(得分:2)
可能。要获取|
之类的shell命令,请使用/bin/bash
作为exec的第一个参数,-c
作为第二个参数,使用整个字符串(包括find
,它的参数和管道等)作为第三个。
Process result = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "/usr/bin/find " + baseDir+"/.. -type f | /usr/bin/grep " +filter1 + | /usr/bin/grep "+filter2+" | /usr/bin/wc -l"});