...
| Code Block |
|---|
| title | back quote exec strings |
|---|
|
// exec "ls -l" to get file listing as a string
var files = `ls -l`
var lines = files.split("\n");
// print only the directories
for (var l in lines) {
var line = lines[l];
if (line.startsWith("d")) // directory
print(line)
}
|
In -scripting mode, nashorn accept shell script style # (hash) single line comments.
| Code Block |
|---|
|
# style line comment -scripting mode
# prints hello
print("hello") |
Nashorn supports shebang scripting. You need to create a link to jjs from your platform executable directory like /usr/bin as shown below.
| Code Block |
|---|
|
$ cd /usr/bin
$ sudo ln -s $JAVA_HOME/bin/jjs jjs
$ chmod 755 test.js
where test.js is as follows:
#!/usr/bin/jjs
print("hello")
$ ./test.js |
For shebang nashorn scripts, -scripting mode is automatically enabled.
Nashorn script API extensions
...