4. Running a script
Now that we can output something to the screen, let’s try and make our example a little more personal. Clear the Groovy Console editor and enter the following:
println "hello, ${args[0]}"
Before we try to run this, let’s look at what’s in the code:
-
printhas becomeprintln- this does the same thing as our previous use ofprintbut adds a new line at the end of the output.- This makes the output easier to read when we’re running on the command line
- Instead of the text
worldwe’re now using${args[0]}:-
argsis a variable (an array1) that holds any command-line arguments we pass to the script- You may have noticed
String[] argsin the Java version ofhello, world- essentially Groovy is writing that segment of code for you. -
args[0]is the first element in theargsarray - this is the first parameter (command-line argument) passed to the script
- You may have noticed
- The
${...}notation tells Groovy that the contents need to the resolved into a value - in this case Groovy needs to determine the value ofargs[0]before displaying the output
-
Don’t worry if this appears to be a big jump from our hello, world - there’s a range of concepts being introduced and we’ll work through them in this tutorial section. For now, put the code into your Groovy Console and know that, when run, your script will say hello to a specified person.
You now need to save your script so go to the File menu and select Save. When prompted, name the file Hello.groovy and save it into a directory you can access.
Unfortunately we can’t run this script in the Groovy Console as it doesn’t provide an option for passing in a command-line parameter. Follow this process to run the script:
- Open a command prompt (terminal) and change to the directory (
cd) into which you savedHello.groovy. - Type the command
groovy Hello.groovy Newmanand press thereturnkey
You should see the following output:
hello, Newman
Of course you can change “Newman” to be any name so feel free to try out your name, the dog’s name etc. However, make sure you add a name - your script needs that parameter or you’ll see a disconcerting error.
- More about arrays in a little bit↩