JavaScript shell scripting

September 3rd, 2009. Tagged: JavaScript

As you probably know, JavaScript is not limited to the browser. There's server-side JavaScript, JS for various extensions, you can script Photoshop operations with JavaScript if you feel like it. Or compile Windows executables. You see where I'm going with this. JavaScript is everywhere :)

And yes, you can do shell scripting in JavaScript. On any platform you can use Rhino to run your scripts. On Windows there's this WSH, Windows Scripting Host you can benefit from, built right into the OS so you don't have to install anything. You can run your JavaScript shell scripts with the cscript utility like:

C:\> cscript jslint.js

And on the Mac there's JavaScriptCore by WebKit. WebKit is not limited to Safari, it's used all over the place on the Mac. So there's a utility called jsc which can run your scripts.

JSC test run

JSC (JavaScriptCore) is well hidden in

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

Check it out, it should be there. And if it is, why not use it via a "shortcut"? So step 1:

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

Step 2... no, there's no step 2, just give it a shot:

$ jsc -h
Usage: jsc [options] [files] [-- arguments]
  -d         Dumps bytecode (debug builds only)
  -e         Evaluate argument as script code
  -f         Specifies a source file (deprecated)
  -h|--help  Prints this help message
  -i         Enables interactive mode (default if no files are specified)
  -s         Installs signal handlers that exit on a crash (Unix platforms only)

So you can just run any JavaScript code on the command line. You can also use jsc as a JavaScript console to type away stuff. Probably not that useful, because you have Firebug console to type into. But still, an option.

$ jsc
> var a = 1;
undefined
> a++
1
> a
2
> 

Shell version of an online tool

Here's an example. Last night I worked on a quick hack to experiment with minifying CSS. There's an online tool over here as a result. Can I run this tool on the command line? Sure.

So imagine you have a JavaScript that parses CSS that looks like this. It defines an object called cssparse. You can use it in a browser-based tool, but also in the command-line version, without any changes. All you need is to create a new file that will be the shell version of the tool, say csspsh.js. In it put the something like:

if (!arguments[0]) {
    print('usage:\n $ jsc csspsh.js "`cat parseme.css`"');
    quit();
}
 
load('cssp.js');
 
print(cssparse.parse(arguments[0]));

You can probably guess but:

  • arguments[] array-like object contains command-line arguments
  • print() prints to the console
  • quit() exits JSC
  • load() loads and executes an external file

How do you pass arguments to your shell script? After a -- delimiter, like so:

$ jsc csspsh.js -- one two three

And since this particular script works with contents of files, I can use cat to read the file and pass it to the script.

$ jsc csspsh.js -- "`cat my.css`"

Shell-script away!

So there you have it. Shell scripting with JavaScript is at your fingertips, either Mac or Windows, or anywhere with Rhino. If you have some cool scripts that you want to run the on the command line, like cron jobs or some automated process, there has never been a better time :)

Tell your friends about this post: Facebook, Twitter, Google+

Sorry, comments disabled and hidden due to excessive spam. Working on restoring the existing comments...

Meanwhile, hit me up on twitter @stoyanstefanov