Skip to content

Instantly share code, notes, and snippets.

@devilelephant
Created September 13, 2013 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilelephant/6554811 to your computer and use it in GitHub Desktop.
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.
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