How Do You Write a Groovy Program to Read in User Input from the Keyboard and Print It Out?

Problem scenario
You want to test out Groovy as a scripting language.  You want a Groovy program to read in user input and print it out to the screen.  How do you do this?

Prerequisite
This assumes that you have already installed Groovy on a Linux server.  If you are running a Red Hat derivative (e.g., CentOS, RHEL, or Fedora), click on this link.  If you are running an Ubuntu or Debian distribution, click on this link for Groovy version 1 and this link for version 2.  If you are using Ubuntu Linux in AWS, click on the version 1 link (because the other directions won't work, and the version 1 link will actually install version 2).

Solution
1.  Create a file called input.groovy with the following two lines:

def input = System.console().readLine 'Please provide some input: '
println "You entered $input"

2.  Run it like this:  groovy input.groovy

Leave a comment

Your email address will not be published. Required fields are marked *