Spidermonkey: JavaScript at the command line

634 views
Skip to first unread message

Robert Citek

unread,
Mar 26, 2009, 2:54:33 PM3/26/09
to CWE-LUG
Can anyone recommend a good tutorial for using JavaScript at the command line?

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

Don Ellis

unread,
Mar 26, 2009, 3:07:25 PM3/26/09
to cwe...@googlegroups.com
Check out this series (unit on JS):


I've played around with some of their other tutorials, and was fairly impressed, though I'd like to go a bit deeper. Nice thing about the tutorials is they are hands on and live. Type in a CSS or HTML (or JS) expression, and it's evaluated right there.

Let me know what you think.

--Don Ellis

Robert Citek

unread,
Mar 26, 2009, 5:33:42 PM3/26/09
to cwe...@googlegroups.com
w3schools seems to be mainly focused on using JS within a web browser.
I'm experimenting to see if JS can be used as a general-purpose
scripting language like perl, ruby, python, bash, etc. Judging from
my Googling, I am suspecting that it cannot, or at least not easily.
The biggest challenge seems to be anything system oriented: file I/O,
system calls, DB connections, etc.

Simple stuff seems to work well enough. For example, this works:

$ js -e 'print("Hello, world");'
Hello, world

As does this when saved as hw.js and permissions changed to +x:

$ cat hw.js
#!/usr/bin/env js
print("Hello, world") ;

$ ./hw.js
Hello, world

But there does not seem to be any JS way of doing the equivalent of this:

#!/usr/bin/env perl
open(FOO, "seq 1 10|") ;
@lines=<FOO>;
chomp(@lines) ;
print join("\t", @lines)."\n" ;

Or is there?

Regards,
- Robert

Don Ellis

unread,
Mar 26, 2009, 5:40:06 PM3/26/09
to cwe...@googlegroups.com
Interesting. One of the origins of JS was as the system programming language in Netscape Web Servers. Certainly had a different name at some point, whose relics remain. I'm trying to remember what JS used to be called before they put Java into the name to introduce a little brand confusion. Should be readily retrieved, and that might be the kind of clue that might help you break into that application domain. [I tried a while back, but I don't remember much progress.]

Maybe you'll need to install Netscape Server to have access to full JS scripting? :P

--Don Ellis
Reply all
Reply to author
Forward
0 new messages