-1

I need to execute the command, ps -aux | awk ' /^user/ { system("pstree " $2) }' But as I can't execute both in one process I would like to execute both in separate processes and redirect the output of ps -aux process as input to awk process.How do I code this? Please help.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • You are looking for: http://docs.oracle.com/javase/7/docs/api/java/nio/channels/Pipe.html or this tutorial: http://tutorials.jenkov.com/java-io/pipes.html – kajacx Jun 01 '14 at 13:38
  • Other possible solution: http://stackoverflow.com/questions/1574837/connecting-an-input-stream-to-an-outputstream – kajacx Jun 01 '14 at 13:41
  • @RichardChambers I want to know how to do this "But as I can't execute both in one process I would like to execute both in separate processes and redirect the output of ps -aux process as input to awk process"not explanation for bash – user3680477 Jun 01 '14 at 14:39

1 Answers1

1

The pipe character (|) connects the standard output of the command to the left to the standard input of the command to the right. So it's already doing what you ask.

If your question is actually related to Java, you probably want the official Pipe class (courtesy @kajacx).

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • Do you mean that I need `java.nio.channels.Pipe` instead of `System.in/out`? – Val Jun 01 '14 at 13:43
  • If you were using `System.in`/`out` you would need to create two *Java* processes communicating in a shell, rather than handling all the piping inside Java. There might be a use case for the former, but I'd recommend to keep all your logic in the same language. – l0b0 Jun 01 '14 at 13:44
  • How creating two java processes impedes handling everything iside java? Why two java processes? Why this is a problem? Why should we do everyting inside one java process when the question is redirecting input of one process to the output of the other? Your response confused me completely. – Val Jun 01 '14 at 13:50
  • 1
    You can either create two Java processes, one running `ps` and another running `awk`, and use the *shell* to pipe the output from one to the other. Or you could use *one* Java process and create a pipe inside that, connecting the `ps` and `awk` commands. I would thoroughly recommend the latter. – l0b0 Jun 01 '14 at 13:56