Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h2>Using Groovy from the command line</h2> <p>The Groovy command line (groovy or groovy.bat) is the easiest way to start using the Groovy Language.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> $groovy -help usage: groovy -a,--autosplit <splitPattern> automatically split current line (defaults to '\s') -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 </pre></td></tr></table> <p>If you have a groovy script, you can edit and run the script immediately.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> $ cat test.groovy println 'Hello Bonson' $ groovy test.groovy Hello Bonson </pre></td></tr></table> <p>Here is an example with your own command line arguments.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> $ cat test.groovy println 'Hello ' + args[0] $ groovy test.groovy Jeeves Hello Jeeves </pre></td></tr></table> <p>However you can also run such a simple groovy program by providing the script in the command line arguments.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> $ groovy -e "println 'Hello Bob'" Hello Bob </pre></td></tr></table> <p>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.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> $ grep -i ^groov /usr/share/dict/words | groovy -e 'print System.in.text.toUpperCase()' GROOVE GROOVELESS GROOVELIKE GROOVER GROOVERHEAD GROOVINESS GROOVING GROOVY </pre></td></tr></table> <p>Because looping through STDIN or input files tends to be a common thing to do, groovy (and ruby, perl etc) provide shortcuts for this. <strong>currently broken, groovy not flushing output (still so 060927?)</strong></p> <p>-n will loop through each line of the input, and provide it to your script in the <em>line</em> variable.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> grep -i ^groov /usr/share/dict/words | groovy -n -e 'println line.toUpperCase()' </pre></td></tr></table> <p>If we definitely want to print the output of each line we can use -p and shorten it to</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> grep -i ^groov /usr/share/dict/words | groovy -p -e 'line.toUpperCase()' </pre></td></tr></table> <p>We can use the looping constructs along with -i, which writes the output back to the original files (and creates a backup copy with the given extension). And wreak havoc on our local file system, with wide-scale search and replace.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -p -i .bak -e '(line =~ "<h\\d>(.*)</h\\d>").replaceAll("$1")' ~/Desktop/cooluri.html </pre></td></tr></table> <p><strong>TIP</strong>: Never ever use the option -i without a backup extension.</p> <p>Or to really get into groovy (literally)</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> find . -name \*.java | xargs groovy -p -i -e '(line =~ "@author James Strachan").replaceAll("@author Bobby Bonson")' </pre></td></tr></table> <p> <br /> Additionally you have access to the line number in the current file you are reading via the variable <em>count</em>. This can be used for a number of convenient groovy one-liners.</p> <p>Let us assume you want to prefix every line in a file with the line number. Doing this requires next to no work in Groovy (we additionally create a copy of the original file with the extension .bak).</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -pi .bak -e "count + ': ' + line" </pre></td></tr></table> <p>Or let us create a grep-like command that prints the line number where it found matching strings for a regular expression.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -p -e "if(line =~ /groovy/)count + ': ' + line" </pre></td></tr></table> <p>Print the first 50 lines of all files:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -p -e "if(count < 50) line" </pre></td></tr></table> <p>until one file is longer than 50 lines:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -p -e "if(count >= 50)System.exit(0);line" </pre></td></tr></table> <p>Add a Groovy-Shebang (the string '#!/usr/bin/groovy') to all Groovy files:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -i .bak -pe "if(count == 1) println '#!/usr/bin/groovy'" *.groovy </pre></td></tr></table> <p>Another very convenient option is -a, which splits the current input line into the array <em>split</em>. By default the split pattern is " " (one space). The option -a optionally takes another split pattern which is then used instead.</p> <p>Print processes owned by root:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ps aux|groovy -a -ne "if(split[0] =~ 'root')println split[10..-1]" </pre></td></tr></table> <p>Print all logins from /etc/passwd that are not commented:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -a':' -ne "if(!(split[0] =~ /^#/))println split[0]" /etc/passwd </pre></td></tr></table> <p>Add the first and the penultimate column of a file:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -a -pe "split[0].toInteger()+split[-2].toInteger()" accounts.txt </pre></td></tr></table> <p>For more examples or inspiration browse through the search results for <a href="http://www.google.com/search?q=perl+one+liners">Perl One Liners</a></p> <h2>listen mode</h2> <p>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).</p> <p>For each connection that is made to this port, groovy executes the supplied script on a line by line basis.</p> <p>This oneliner will reverse every line that is thrown at it, try telnet to your machine on port 1960 to interact with this script.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -l -e "println line.reverse()" </pre></td></tr></table> <p>you can combine the -p option from earlier, to automatically print the result of your script</p> <p>The following one liner is equivalent to the one liner immediately above.</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -l -p -e "line.reverse()" </pre></td></tr></table> <p>More examples of useful command line scripts in <a href="http://svn.groovy.codehaus.org/browse/groovy/trunk/groovy/groovy-core/src/examples/commandLineTools">SVN</a></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced