Java Reference
In-Depth Information
It
can
interpolate
too
-
$
{
hw
}
EOSTR
print
(
output
);
Nashorn helper functions
Nashorn also provides some helper functions to make it easier for developers to
accomplish common tasks that shell scripts often want to perform:
print()
/
echo()
We've been using
print()
throughout many of our examples, and these func‐
tions behave exactly as expected. They print the string they've been passed, fol‐
lowed by a newline character.
quit()
/
exit()
These two functions are completely equivalent—they both cause the script to
exit. They can take an integer parameter that will be used as the return code of
the script's process. If no argument is supplied, they will default to using 0, as is
customary for Unix processes.
readLine()
Reads a single line of input from standard input (usually the keyboard). By
default, it will print the line out on standard output, but if the return value of
readLine()
is assigned to a variable, the entered data will end up there instead,
as in this example:
print
(
"Please enter your name: "
);
var
name
=
readLine
();
print
(
"Please enter your age: "
);
var
age
=
readLine
();
print
(<<
EOREC
);
Student
Record
-+-+-+-+-+-+-+-
Name:
$
{
name
}
Age:
$
{
age
}
EOREC
readFully()
Instead of reading from standard input,
readFully()
loads the entire contents
of a file. As with
readLine()
, the contents are either printed to standard out‐
put, or assigned to a variable:
var
contents
=
readFully
(
"input.txt"
);
load()
This function is used to load and evaluate (via JavaScript's
eval
) a script. The
script can be located by a local path, or a URL. Alternatively, it may be defined
as a string using JavaScript's
script object
notation.