Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Tuesday, September 17, 2013

Embedding groovy with groovyShell

//see: http://groovy.codehaus.org/Embedding+Groovy

Binding binding = new Binding();
binding.setVariable("foo", 2);
GroovyShell shell = new GroovyShell(binding);

Object value = shell.evaluate("println 'Hello World!'; x = 123; return foo * 10");
assert value.equals(20);
assert binding.getVariable("x").equals(123);

Thursday, February 16, 2012

Shell/Command line execution

//def cmd = "cmd /C dir"
def cmd = ["cmd","/C","dir g*.bat"] // more flexible format

//Groovyish Java//
Runtime r = Runtime.runtime
Process p = r.exec(cmd.toArray(new String()))
BufferedReader inp = new BufferedReader(new InputStreamReader(p.getInputStream()))
while (line = inp.readLine()) { println line }

//Groovy!//
println cmd.execute().text

Monday, November 7, 2011

Command line input


def input = System.console().&readLine
def pwd = System.console().&readPassword

String username = input("username: ");
String password = pwd("password: ");