0

I want to run this command using 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) 

I have tried the following:

// 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)\""};

I am using args like this: processBuilder.command(args);

5
  • Updated my question. I want to redirect the output from several zcat commands to the sort. Commented Jun 12, 2016 at 3:28
  • ProcessBuilder is not a shell. either invoke the shell explicitly or do the redirection yourself. Commented Jun 12, 2016 at 3:59
  • This is not a duplicate. The problem here is different. I did invoke the shell explicitly in my second attempt. Commented Jun 12, 2016 at 6:35
  • 2
    First, remove internal quotes around sort .... Second, I don't think sh understands <(...) syntax - it's more of a bash thing. Commented Jun 12, 2016 at 12:05
  • You are correct! I figured this out a few hours after I posted the question but could not add an answer since this question was marked duplicate. Commented Jun 12, 2016 at 22:00

1 Answer 1

0

I finally figured it out. As Roman has mentioned in his comment, sh does not understand redirection so I had to use bash. I also had to consume both the input stream and the error stream.

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();
Sign up to request clarification or add additional context in comments.

1 Comment

You used process.getInputStream() twice. Second one should be error stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.