2016-06-12 3 views
0

에서 리디렉션을 사용할 때이 ProcessBuilder를 사용하여이 명령을 실행하려는 ProcessBuilder를 사용하는 방법 : 다음 나는 시도리눅스

sort -m -u -T /dir -o output <(zcat big-zipped-file1.gz | sort -u) <(zcat big-zipped-file2.gz | sort -u) <(zcat big-zipped-file3.gz | sort -u) 

을 :이 같은 args을 사용하고

// This doesn't recognise the redirection. 
String[] args = new String[] {"sort", "-m", "-u", "-T", "/dir", "-o", "output", "<(zcat big-zipped-file1.gz | sort -u)", "<(zcat big-zipped-file2.gz | sort -u)", "<(zcat big-zipped-file3.gz | sort -u)"}; 

// This gives: 
// /bin/sh: -c: line 0: syntax error near unexpected token `(' 
String[] args = new String[] {"/bin/sh", "-c", "\"sort -m -u -T /dir -o output <(zcat big-zipped-file1.gz | sort -u) <(zcat big-zipped-file2.gz | sort -u) <(zcat big-zipped-file3.gz | sort -u)\""}; 

: processBuilder.command(args);

+0

내 질문이 업데이트되었습니다. 여러 zcat 명령의 출력을 정렬로 리디렉션하고 싶습니다. –

+0

ProcessBuilder가 셸이 아닙니다. 쉘을 명시 적으로 호출하거나 리디렉션을 직접 수행하십시오. – jtahlborn

+0

이것은 중복되지 않습니다. 여기의 문제는 다릅니다. 두 번째 시도에서 명시 적으로 쉘을 호출했습니다. –

답변

0

나는 그것을 마침내 발견했습니다. 로마가 언급 한대로 sh은 리디렉션을 인식하지 못하므로 bash을 사용해야했습니다. 또한 입력 스트림과 오류 스트림을 모두 소비해야했습니다.

String[] args = new String[] {"/bin/bash", "-c", "sort -m -u -T /dir -o output <(zcat big-zipped-file1.gz | sort -u) <(zcat big-zipped-file2.gz | sort -u) <(zcat big-zipped-file3.gz | sort -u)"}; 

ProcessBuilder builder = new ProcessBuilder(); 
builder.command(args); 
Process process = builder.start(); 
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); 
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
while((line = input.readLine()) != null); 
while((line = error.readLine()) != null); 

process.waitFor(); 
+0

'process.getInputStream()'을 두 번 사용했습니다. 두 번째는 오류 스트림이어야합니다. – Roman

+0

감사합니다. 고쳤다! –