Activate your free membership today | Log-in

Thursday, September 3rd, 2009

JavaScript on the command line or as a shell scripting language

Category: Java, JavaScript

I am always fascinated that no matter how shiny the OS, sooner or later you'll end up typing things into a text console to get the darn machine to do what you want it to. To make matters worse you also have to resort to languages that you don't use every day but are from cryptic days of development where keyboards obviously had more interesting characters like ~ | -- and others easily available and scripts had to have numerous | in any line of code.

Enter Stoyan Stefanov who wrote a neat little blog post explaining how you can use JavaScript as a shell language.

In essence, Rhino is the answer for *nix machines but if you cuddle the penguin you most likely are a shell master anyways.

If your world features Gates and Windows, then there is the Windows Scripting Host, which has a tool to execute JavaScript on the DOS shell:

JAVASCRIPT:
  1. C:\> cscript jslint.js

Macs have a tool called JSC which is "conveniently" located in:

JAVASCRIPT:
  1. /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

To make it human usable, creating a shortcut is a good plan:

JAVASCRIPT:
  1. $ sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc

You can then execute JavaScript by starting JSC which gives you something like the Firebug JS console in the OS console (without the console is not defined messages *in* the console).

With a few more lines using the print(), quit() and load() commands available on the shell you can then use any JavaScript to mess with your files and folders.

Posted by Chris Heilmann at 7:25 am
16 Comments

++++-
4.2 rating from 21 votes

16 Comments »

Comments feed TrackBack URI

though the markup and is there… the two longer pre tags you used appear truncated in this post

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

$ sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc

Comment by naterkane — September 3, 2009

I use Rhino on the shell all the time – when I want an interactive JavaScript interpreter. But this is a far cry from having the whole ecosystem of Unix shell commands available to you for your pipelining pleasure. If you want to get around on your system, you need to learn a shell!

Comment by sentientholon — September 3, 2009

Any pointer on messing with files and folders ? Some FileUtils equivalent?

Comment by jpjoyal — September 3, 2009

Anybody here saavy to why this does not work as expected?

create file foo:

#!/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
print(’hello world’);

chmod 755 foo

./foo

no output… ajaxian?!

Comment by zachstronaut — September 3, 2009

FWIW, I think there’s also a readline() built-in for grabbing data from STDIN. So, no native file access, but you can pipe things in. That’s useful at least for using things like JSLint

Comment by lmorchard — September 3, 2009

(and by readline(), I mean readline() in jsc)

Comment by lmorchard — September 3, 2009

IMHO, if you need a command-line scripting language, install Python. It has cleaner syntax than Javascript, but has so many similarites that a JS programmer could be comfortable with it in an afternoon.

Comment by tercero12 — September 3, 2009

By default, WSH (Windows Script Host) only executes JavaScript from a file. I’ve written some JavaScript that makes WSH act like an interactive interpreter (read-eval-print loop). There’s a page describing it at: http://waterken.sourceforge.net/web_send/wsh/ The really cool thing about running JavaScript this way is that you’re not confined by the Same Origin Policy, so you can script across web sites. Think of it as a shell for the Web, rather than a shell for your local box.

Comment by TylerClose — September 3, 2009

There’s been a JavaScript OSA for the Mac since 2001.

LateNightSoftware

Comment by chasd — September 3, 2009

@zachstronaut

I tried doing that too, is probably because jsc is trying to execute the script including the first line with the shebang and stops because it’s not valid js, I haven’t found an alternative, but it would be really cool if we could have js shell scripts.

Comment by reaktivo — September 3, 2009

If you’re looking for a scripting language with simplicity and power look at rebol. It’s closer to Javascript than you may think see
http://reboltutorial.com/blog/rebol-javascript/

For example you will be able to tweet in one line in command line:
http://reboltutorial.com/blog/rebol-twitter-update/

Here’s tips for improving productivity with command line
http://reboltutorial.com/blog/rebol-console-directly/

Comment by rebtut — September 4, 2009

Narwhal aims to be a complete environment for JavaScript on the server, including a standard library (implementing the CommonJS/ServerJS spec), package manager, shell, virtual environments, etc.

It’s a layer on top of the interpreters, so you’ll be able to switch between Rhino, V8, JSC, etc. For now Rhino is the most complete.

It’s still a work in progress, but useful none the less: http://narwhaljs.org/quick-start.html

Comment by tlrobinson — September 5, 2009

JavaScript OSA is a port of the Mozilla JavaScript scripting system to the Macintosh:

http://www.latenightsw.com/freeware/JavaScriptOSA/

Comment by randomrandom — September 5, 2009

This is very interesting as well –

JSTalk is how I imagine AppleScript would be done today, if it was written by a Cocoa programmer who was smitten by Cocoa and Objective-C…

http://github.com/ccgus/jstalk

Comment by randomrandom — September 5, 2009

January 1999 Motorola 328cAlthough plus size mother of the bride dressesthe first flip phone in should be sooner, but this clamshell phone is just an integrated microphonemother of bride dresses is not in the usual sense we are now referred to flip.Column Strapless Satin Tulle Wedding Dress Most of the time we are actually referring to folding clamshell handsets, but Motorola’s first cheap prom dressesclamshell phone is the famous “palm-sized” – 328c. The phone is the beginning of in the domestic market and ghetto prom dressescan be received in English and Chinese text, but it does not support sending text messages in Chinese, which is sorry. Column Satin Organdie Lace Wedding DressHowever, clear call quality and handsome appearance are popular at the time this phone place.wedding dress

Comment by wuwei — November 10, 2009

Give your laptop new life by replace a new high quality laptop battery.
Battery for MSI WIND U100 U-100 BTY-S11 BTYS11 AKKU
9cell Batterie pour Medion Akoya Mini E1210 E 1210 UMPC
Batterie pour MSI Wind U100,MS-N011,U100W-085NL,UMPC
Battery For MSI MS1013 MS1057 MS1058 MS-1012 BTY-S25
BATTERY for MSI MegaBook MS1013 MS-1057 1058 S250 ACCU
Battery For MSI MS-1058 MS1006 MS1012 MS1013 MS1057
Genuine MS1013 Battery MSI Megabook MS-1013 series
Genuine MSI MegaBook MS-1013 Battery MS1013 4400mAh
Genuine MS1006 MS1012 Battery MSI S250 S260 S262 S270 pink
Original Akku MSI MegaBook S271 S262 S270 S250 MS-1006
Original Akku MEDION MD95020 MD96400 MS1006 MS1012 wei?
New Original BTY-S25 Battery for MSI PR200 PR201 PR210
Battery MSI MegaBook M510 M510A M510BM 510C MS1003
MSI MegaBook M510 MS-1003 BATTERY MS1003 925C2050 Black
Akku MSI L710 L715 L720 L725 L730 L735 L740 L745 M520
Akku Medion MD95597 SIM2040 BTY-L71 LG K1 MSI L720
Akku MSI L710 L715 L720 L725 L730 L735 L740 L745 M520

Comment by cheapbattereis — December 29, 2009

Leave a comment

You must be logged in to post a comment.