Created
September 13, 2013 19:12
-
-
Save devilelephant/6554811 to your computer and use it in GitHub Desktop.
An easy way to do Unix piping when executing Unix commands from Java or Groovy In unix you can issue a command like: sh -c 'ls -l | sort" which is calling the 'sh' shell with a command and returning the result. I wrote the example in groovy but the trick works with Runtime.exec(String[] cmd) as well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cmd = ['sh', '-c', 'ls -l | sort'] | |
def p = cmd.execute() | |
println p.text | |
// Java would be something like | |
// String[] cmd = { "sh", "-c", "ls -l | sort" }; | |
// Process p = Runtime.getRuntime().exec(cmd); | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment