This is the contents of Groovy's command line help:
C:\>groovy -help usage: groovy [options] [args] options: -a,--autosplit <splitPattern> split lines using splitPattern (default '\s') using implicit 'split' variable -b,--basescript <class> Base class name for scripts (must derive from Script) -c,--encoding <charset> specify the encoding of the files -classpath <path> Specify where to find the class files - must be first argument --configscript <arg> A script for tweaking the configuration options -cp,--classpath <path> Aliases for '-classpath' -D,--define <name=value> define a system property -d,--debug debug mode will print out full stack traces --disableopt <optlist> disables one or all optimization elements. optlist can be a comma separated list with the elements: all (disables all optimizations), int (disable any int based optimizations) -e <script> specify a command line script -h,--help usage information -i <extension> modify files in place; create backup if extension is given (e.g. '.bak') --indy enables compilation using invokedynamic -l <port> listen on a port and process inbound lines (default: 1960) -n process files line by line using implicit 'line' variable -p process files line by line and print result (see also -n) -v,--version display the Groovy and JVM versions
From the help contents, we could see that the -e option can be used to run a simple groovy program by passing the script as String in the command line arguments.
Here is a simple example that echoes something on the screen:
C:\> groovy -e "println 'Hello World" Hello BobAs you could see, the String Hello World was displayed. This may look very simple but it is very useful if you are new to Groovy.
The -e option is also useful for chaining programs together. Be it on the Windows or *NIX platform, this is something that can help you.
Here is an example:
C:\>groovy -e "println ((1..5).join(' banana\n'))" 1 banana 2 banana 3 banana 4 banana 5 C:\>groovy -e "println ((1..5).join(' banana\n'))" | find "2" 2 banana
As you could see in the second command, the output of the groovy console was sent to the find command. The find command then tried to find a line that has the text 2.
Here is another example
C:\>copy con fruits.txt appLe Banana CheRry^Z 1 file(s) copied. C:\>type fruits.txt appLe Banana CheRry C:\>type fruits.txt | groovy -e "print System.in.text.toUpperCase()" APPLE BANANA CHERRY C:\>
This shows that the Groovy program can be the recipient of the output from another command.
The *NIX platform can give a lot of options for pipes as there are many rich built-in commands to choose from to construct a complex behavior.
C:\>groovy -e "println ((1..10).join(', '))" > numbers.txt C:\>type numbers.txt 1, 2, 3, 4, 5, 6, 7, 8, 9, 10The contents of the file numbers.txt came from the output of the Groovy script.
C:\>copy con animals.txt Cat doG ElepHant^Z 1 file(s) copied. C:\>type animals.txt Cat doG ElepHant C:\>type animals.txt | groovy -n -e "println count + '. ' +line.toUpperCase()" 1. CAT 2. DOG 3. ELEPHANT C:\>type animals.txt | groovy -n -e "println count + '. ' +line.toLowerCase()" 1. cat 2. dog 3. elephantThe count variable contains the index of the line starting from 1 while the line variable contains the line String value.