Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Using Groovy from the command line

The Groovy command line (groovy or groovy.bat) is the easiest way to start using the Groovy Language.

$groovy -help
usage: groovy
 -c,--encoding <charset>   specify the encoding of the files
 -e <script>               specify a command line script
 -h,--help                 usage information
 -i <extension>            modify files in place
 -l <port>                 listen on a port and process inbound lines
 -n                        process files line by line
 -p                        process files line by line and print result
 -v,--version              display the Groovy and JVM versions

If you have a groovy script, you can edit and run the script immediately.

$ cat test.groovy 
println 'Hello Bonson'

$ groovy test.groovy 
Hello Bonson

However you can also run such a simple groovy program by providing the script in the command line arguments.

$ groovy -e "println 'Hello Bob'"
Hello Bob

This may not look useful, but it fits in with the UNIX tradition of chaining simple programs together to build powerful commands. Tools like perl, sed, awk and grep do these jobs very well. But many users have limited experience with these tools' arcane syntax and will be more familiar with Java and therefore Groovy.

$ grep -i ^groov /usr/share/dict/words | groovy -e 'print System.in.text.toUpperCase()'
GROOVE
GROOVELESS
GROOVELIKE
GROOVER
GROOVERHEAD
GROOVINESS
GROOVING
GROOVY

Because looping through STDIN or input files tends to be a common thing to do, groovy (and ruby, perl etc) provide shortcuts for this. currently broken, groovy not flushing output

-n will loop through each line of the input, and provide it to your script in the line variable.

grep -i ^groov /usr/share/dict/words | groovy -n -e 'println line.toUpperCase()'

If we definately want to print the output of each line we can use -p and shorten it to

grep -i ^groov /usr/share/dict/words | groovy -p -e 'line.toUpperCase()'

We can use the looping constructs along with -i, which writes the output back to the original files. And wreak havoc on our local file system, with wide-scale search and replace.

groovy -p -i .bak -e '(line =~ "<h\\d>(.*)</h\\d>").replaceAll("$1")' ~/Desktop/cooluri.html

Or to really get into groovy (literally)

find . -name \*.java | xargs groovy -p -i -e '(line =~ "@author James Strachan").replaceAll("@author Bobby Bonson")'

For more examples or inspiration browse through the search results for Perl One Liners

listen mode

Another groovy command line option is the ability to startup groovy in listen mode, which will attach groovy to a TCP port on your machine (-l <port> with a default port of 1960).

For each connection that is made to this port, groovy executes the supplied script on a line by line basis.

This oneliner will reverse every line that is thrown at it, try telnet to your machine on port 1960 to interact with this script.

groovy -l -e "println line.reverse()"

you can combine the -p option from earlier, to automatically print the result of your script

The following one liner is equivalent to the one liner immediately above.

groovy -l -p -e "line.reverse()"

More examples of useful command line scripts in CVS

  • No labels