sh.js is a JavaScript library for Unix shell scripting working on node.js.
For example, to sort user names in /etc/passwd
in a classical shell script, you could write:
cut -f1 -d: /etc/passwd | sort
With sh.js, the following statement would accomplish the same:
sh('cut -f1 -d: /etc/passwd')('sort');
You may find sh.js useful if:
Run a command:
sh('echo hello');
Change the working directory and run a command in the new directory:
sh.cd('/').and('ls -l');
In a Git repository, put all authors with their email in a file:
sh('git log --pretty=format:"%aN %aE"')
('uniq')('sort').file('AUTHORS');
Report the space availability of the root partition to the remote monitor.lan
server:
sh('df')('awk \'{ if ($6 == "/") printf "%s", $5 }\'')
.result(function(available) {
sh('curl -d root="' + available + '" https://monitor.lan/disk_report');
});
For more examples, head to the tutorial.
As of now, sh.js still has some serious limitations:
Stream
s are not supported. You can't easily redirect standard output to an HTTP stream for instance. This is likely to be resolved but I'm trying to figure out what the right API should be.