This post originated from an RSS feed registered with Java Buzz
by Jeremy Rayner.
Original Post: grash: a unix-like shell for your JVM
Feed Title: javanicus
Feed URL: http://www.javanicus.com/blog2/subjects/java-rss.xml
Feed Description: Jeremy Rayner on java and other stuff.
"If everything in Unix is a file and everything in Java is an Object, wouldn't it be nice if you could explore your Objects in the JVM with the same powerful mechanisms you use in Unix."
My line of thought a couple of weeks ago has led to a small implementation of a shell for the JVM, which I have named grash.
The name is derived from the fact that it is based in part on the externally observed behaviour of the bash shell, and that it is written in and exposes to the user the Groovy programming language.
Hence 'GRoovy-Again SHell'Using the concept of a 'current working instance' in place of a 'current working directory' some of the tools in a normal shell already make some sense.what follows has been adapted from the wonderful Kernighan and Pike
What Objects are out there?
The ls command lists the names (not contents) of Objects:
/ grash$ ls
junk
main
temp
/ grash$
The names are sorted into alphabetical order automatically.ls , like most commands, has options that may be used to alter its default behavior. The -l option gives a "long" listing that provides more information about each Object:
/ grash$ ls
-------rw- junk
--------x main
------rw- temp
/ grash$
The string - - - - - - r w - tells who has permission to read and write the Object; in this case there is a public getter and setter for the junk and temp objects.
Printing Objects - cat
Now that you have some Objects, how do you look at their contents? The simplest of all the printing commands is cat . cat prints the contents of the Object name by its argument:
/ grash$ cat junk
To be or not to be
/ grash$ cat temp
That is the question.
/ grash$
A handful of useful methods
Grash exposes the Java and Groovy Libraries to the command line user, providing
/ grash$ cat junk
To be or not to be
/ grash$ cat junk.length()
18
/ grash$ cat junk.tokenize()
[To, be, or, not, to, be]
/ grash$ cat junk.tokenize().sort()
[To, be, be, not, or, to]
/ grash$
Pipes
A pipe is a way to connect the output of one expression to the input of another. I have implemented pipelines in the simplest way at the moment.
/ grash$ ls -a | println size()
11
/ grash$
The expressions in a pipeline currently evaluate one after another, but in order to behave like Unix, concurrent evaluation of the commands in a pipeline would be desired.The methods are currently called on the result of the previous evaluation, again this should perhaps be a different mechanism, using the current working instance and others.