Grails Cookbook - A collection of tutorials and examples

Groovy -e Command Line Option

This post will show how to use Groovy's -e command line option. The Groovy command line tool provides an interactive console to help you learn the Groovy Language. The Groovy -e option is very useful as it gives a quick way for executing simple Groovy scripts.

Description

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.


Examples

Simple Echo Example

Here is a simple example that echoes something on the screen:

C:\> groovy -e "println 'Hello World"
Hello Bob

As 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.

Pipe Example

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.

Redirection Example

Similar to pipes, we can use this to send the output of the Groovy script to a file.
C:\>groovy -e "println ((1..10).join(', '))" > numbers.txt
C:\>type numbers.txt
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
The contents of the file numbers.txt came from the output of the Groovy script.
With -n optionExample
Another common case is to use -e together with the -n option. The -n option will loop through each line of the input, and provide it to your script in the line variable. The count variable is also available for the 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. elephant
The count variable contains the index of the line starting from 1 while the line variable contains the line String value.

Other Groovy Tutorials