Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Groovy Scripting and Command-Line Usage

15. Groovy Scripting and Command-Line Usage

Groovy is not only a versatile programming language but also a powerful scripting language. You can write Groovy scripts to automate tasks, perform system administration, and manipulate data from the command line. This tutorial covers how to create Groovy scripts and execute them from the command line.

Creating a Groovy Script:

A Groovy script is a plain text file with a '.groovy' extension. To create a Groovy script, you can use any text editor or integrated development environment (IDE). For example, create a file named 'myscript.groovy':

// myscript.groovy

def message = "Hello, World!"
println(message)
 

In this example, we create a simple Groovy script that prints "Hello, World!" to the console.

Running a Groovy Script:

You can execute Groovy scripts from the command line using the 'groovy' command followed by the script's filename.

Example: Running a Groovy Script
groovy myscript.groovy
 

Executing this command will run the 'myscript.groovy' script, and you will see the output "Hello, World!" in the terminal.

Passing Command-Line Arguments:

You can pass command-line arguments to a Groovy script using the 'args' variable.

Example: Using Command-Line Arguments
// myscript.groovy

if (args.length == 0) {
    println("Please provide a name as a command-line argument.")
} else {
    def name = args[0]
    println("Hello, $name!")
}
 
Now, you can run the script with an argument:
groovy myscript.groovy Alice
 

This will print "Hello, Alice!" to the console.

Interactive Mode:

Groovy provides an interactive mode called the Groovy Shell ('groovysh' or 'groovyConsole'), which allows you to execute Groovy code interactively, similar to a REPL (Read-Eval-Print Loop).

Example: Using Groovy Shell
groovysh
 

Once in the Groovy Shell, you can enter Groovy code line by line and see the results immediately.

Script Shebang:

On Unix-based systems (Linux and macOS), you can use a shebang line at the beginning of a Groovy script to make it executable directly from the command line.

Example: Adding a Shebang Line
#!/usr/bin/env groovy

def message = "Hello, World!"
println(message)
 

After adding the shebang line and making the script executable with 'chmod +x myscript.groovy', you can run it like this:

./myscript.groovy
 

Working with Files:

Groovy scripts can read and write files, making them suitable for various automation tasks.

Example: Reading a File
// readfile.groovy

def filename = args[0]
def file = new File(filename)

if (file.exists()) {
    def content = file.text
    println("File Content:\n$content")
} else {
    println("File not found: $filename")
}
 
 To read a file using this script:
groovy readfile.groovy mydata.txt
 

Conclusion:

Groovy scripting and command-line usage are valuable for automating tasks, processing data, and performing various system-related activities. Whether you need to create custom utilities or automate repetitive tasks, Groovy's simplicity and expressiveness make it an excellent choice for scripting on the command line. You can easily integrate Groovy scripts into your daily workflow to streamline processes and save time.