Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added grapes enabled examples

...

Code Block
@Grapes([
 @Grab(group='org.codehaus.groovy.modules.groosh',module='groovy-groosh',version='[0.3.5,)'),
 @GrabConfig(systemClassLoader=true)
])

def gsh = new import groosh.Groosh
Groosh.withGroosh(this);
gsh.
ls().toStdOut();
 >> stdout

Pre-requisites

Groosh is based on Groovy 1.7.0 or higher.

...

The following example shows Groosh in action:

Code Block
//Read a text file and write it to stdout
@Grapes([
 @Grab(group='org.codehaus.groovy.modules.groosh',module='groovy-groosh',version='[0.3.5,)'),
 @GrabConfig(systemClassLoader=true)
])

def gsh = new import groosh.Groosh
Groosh.withGroosh(this);

gsh.cat('test_scripts/blah.txt').toStdOut();
 >> stdout

Another example :

Code Block
//Count the lines of all .java files in this directory and all its subdirectories.
//Write the total number of lines to stdout.

@Grapes([
 @Grab(group='org.codehaus.groovy.modules.groosh',module='groovy-groosh',version='[0.3.5,)'),
 @GrabConfig(systemClassLoader=true)
])

def gsh = new import groosh.Groosh
Groosh.withGroosh(this);

def f = gsh.find('.', '-name', '*.java', '-ls');
def total = 0;
def lines = gsh.grid { values,w |->
  def x = values[2,4,6,10];
  def s = x.join('	');
  w.println(s);
  def total += Integer.parseInt(values[6]);
};

f.pipeTo(lines); | lines
lines.toStdOut(); >> stdout
println "Total: ${total}"
+ tota);

Sometimes the name of a shell command conflicts with a Groovy method (for example ''grep''). This means that

...