Nutshell in a nutshell

Without knowing anything about Nutshell, guess what the following command outputs:

If you guessed that it displays only those files in the current directory that are less than 2000 bytes in size, that would be correct. But that wasn’t too hard, was it? The syntax is quite intuitive. Now compare it what you would really have to type in bash:

Show this command to a seasoned Unix guru. Even he can’t decipher this command without looking at the output of ls -l first.

So what makes performing the same command in Nutshell more intuitive. Well, first of all, Javascript has more syntactic sugar than bash script. Secondly, there is something really sweet going on behind the scenes. When you chain two unix commands together with a pipe, the textual output of the first is passed on as an input to the second. But if the second command is to do anything useful with this input, it has to go and parse it first. That’s what the awk command is doing. It is splitting the output of ls -l ,taking the 5th token of each line, performing the comparison, and printing the whole line if the test succeeds. This is where shell scripting is unproductive. It’s all about parsing output.

So how does Nutshell solve this? By using a technique called object piping. When you type in ls -l in the Nutshell prompt you are actually running a Javascript wrapper of the same unix command. This wrapper does all the hard work of parsing the output for you. But in addition to that it also creates and returns an object representation of the output. When you pipe together commands in Nutshell, the successive commands recieve the piped object. So all you need to do then is access the desired fields of the object and produce your own result object. No more parsing to deal with. The ls command outputs an object which looks like this:

All the filter command needs to do is evaluate the function passed to it as an argument, run it over the list, and call the toString over each object. Simple, intuitive and saves a couple of hours of hunting though man pages of awk and find. The aim of Nutshell is to be fully compatible with bash. So you can also use traditional Unix commands in a pipe. Objects are turned into strings by calling their toString methods before being passed to a Unix command. So, you are into command-line masochism, you can write commands like this in Nutshell:

If you like the idea of an object oriented shell with Javascript as the scripting language. Grab a copy of the source from Github and give it a test drive.