파이프로 연결된 명령 행 프로그램을 호출하고 있습니다. 이 모든 것은 Linux에서 확실히 작동합니다. Java exec()가 파이프 연결된 명령의 예상 결과를 반환하지 않습니다.
내 방법 :protected String execCommand(String command) throws IOException {
String line = null;
if (command.length() > 0) {
Process child = Runtime.getRuntime().exec(command);
InputStream lsOut = child.getInputStream();
InputStreamReader r = new InputStreamReader(lsOut);
BufferedReader in = new BufferedReader(r);
String readline = null;
while ((readline = in.readLine()) != null) {
line = line + readline;
}
}
return line;
}
좀 고양이 파일을 호출하고있어 경우 | grep asd, 예상되는 결과가 나타납니다. 그러나 모든 명령이 올바르게 작동하는 것은 아닙니다.
cat /proc/cpuinfo | wc -l
나이 :이와 예를 들어
cat /proc/cpuinfo | grep "model name" | head -n 1 | awk -F":" '{print substr($2, 2, length($2))}
방법은 null를 돌려줍니다. 나는이 문제를 짐작하고있다. 머리, 꼬리, wc 등 머리글을 출력 형식에 따라 다릅니다. 어떻게이 문제를 해결하고 출력 결과를 얻을 수 있습니까?
는 cat''의 (* 3 사용)이 시도. –
@Dennis - 실제로. 잘 목격되었다! –
@Bobby 시도해 보았지만 도움이되지 않습니다. – Pawka