Most JavaScript (JS) tutorials are written for using JS within a
browser. But JS can also be used from the command line if you install
the stand-alone JS interpreter Spidermonkey:
$ sudo apt-get install spidermonkey-bin
The canonical simple example:
$ js -e 'print("Hello, world") ;'
Hello, world
And a simple line numbering loop:
$ yes "Hello, world" | head | js -e 'var count=0 ; while
(line=readline()) { count++ ; print(count, ": ", line) ; } '
1 : Hello, world
2 : Hello, world
3 : Hello, world
4 : Hello, world
5 : Hello, world
6 : Hello, world
7 : Hello, world
8 : Hello, world
9 : Hello, world
10 : Hello, world
Unfortunately, it took me a bit of digging and guessing to find out
how to do even those simple examples. For example, the readline()
function isn't even mentioned in the "Javascript: the Definitive
Guide" book. I stumbled upon that by looking at this Mozilla page:
https://developer.mozilla.org/en/Introduction_to_the_JavaScript_shell
What I'm looking for is more in line with this tutorial on awk, a
series of one- or two-liners that get across the basic functionality
of the language:
http://www.vectorsite.net/tsawk_1.html#m2
I'm still Googling, but can anyone recommend a good JS or Spidermonkey tutorial?
Regards,
- Robert