View on GitHub

Binjs

#!/bin/js is a scripting framework using the best of bash and JavaScript

Download this project as a .zip file Download this project as a tar.gz file

/bin/js scripts are a mash-up of JavaScript and bash.

Simple bash code is valid

#!/bin/js
#
#  My first /bin/js script
#

echo Hello World

Simple JavaScript code is valid.

#!/bin/js
//
// If you prefer use JS style comments
//

for (var i = 0 ; i < 23 ; i++) {

    $.print("Hello World");

}

And you can mash-up both.

#!/bin/js
/**
 * Another comment style
 */

for (var i = 0 ; i < 23 ; i++) {

    echo Hello World

}

Pipes and redirects and all the wonders of bash one-liners are available.

#!/bin/js

cat "${HOME}/apache.log" | tail -20 | awk ' {print $7}' | tee.tail.log

All the beauty(ehem) of C style syntax from JavaScript is available.

#!/bin/js

...

switch (errno) {

    case 1 : {
        doThing();
        break;
    case 2 :
        echo found a wrongun >> errs.log
        break;
    default :
        $.error(10, "Unknown option");
}    

switch statements, if/else, while loops, do/while loops, try/catch/finally and others from JavaScript are used, since the bash ones are really ugly.

The pipes and redirects and even the trailing & from bash are used.

Simple Usage

/bin/js syntax should be pretty straight forward if you write JavaScript and are used to working with Linux. The goal is that syntax should be completely natural for developers who are comfortable with C style languages such as JavaScript, C, C++, C# or Java, and work on the bash command line.

The general rule with /bin/js syntax is that each line of the file is interpreted as either JavaScript OR a SINGLE line of bash, but not both.

Thus you can not write

if (true) echo Hello World

You have to write the statements on separate line

if (true) {
    echo Hello World
}

Var statements and functions must be written on a single line, and var is required in /bin/js scripts.

var myVal = new Object();

function myFunc() {

    echo Hello World

}

Variables defined this way with function or var are then known to the parser and can be called as JavaScript.

myVal.attribute = 23;
myFunc();

Global vars are NOT supported

globalVar = 23;  // wrong, missing vars

Mutli-line vars are NOT supported

var i,j,k;       // wrong only i is known

In short, if you don't create a var or a function with the var and function statements at the start of the line the preparser will not treat them as JavaScript.

if (true) var funk = function() {return true;}

funk(); // wrong, parse does not know about "funk" so it is interpreted as bash

Just remember, each line is interpreted as EITHER JavaScript OR bash by a preparser which sends the lines to the correct interpreter. The preparser never compiles the code, it just peaks at the front of the line to see weather v8 or bash should compile it.

The net result of this is a very comfortable way to write scripts, the final code is self-explanatory and natural, you can increment a variable in a script with i++ instead of i=$((i + 1)). The code you write looks like code, and the lines of bash commands look exactly like you would type them on the command line.

If you know how to write a bit of JavaScript and have minimal bash skills you already know how to write /bin/js scripts. Just Add #!/bin/js as the first line of a text file and make it executable with chmod.

The framework is designed from the ground up for writing command line scripts on Linux. This video shows a command line Matrix screensaver written as a single /bin/js script in less that 100 lines of code.

http://youtu.be/ilR6TeBfP8o

And this is a star field screensaver in 50 lines of code.

http://youtu.be/Ea0GtO18FSY

Documentation

Docs generated from source are available here http://code.abnoctus.com/binjs/CoverPage.html

API docs are available here http://code.abnoctus.com/binjs/api/ Full details of the parser rules are in the documentation under ParserRules.md.

N.B. /bin/js is a Linux project, it might compile on a Mac or BSD or other versions of Unix, but it will not run on Windoze.