如何在 java Runtime.exec 中使用管道
How to use pipes in a java Runtime.exec
此命令行的输出为空。
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();
是管道“|”吗?
要获得像 |
这样的 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"});
此命令行的输出为空。
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();
是管道“|”吗?
要获得像 |
这样的 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"});