Jeremy Rayner on java and other stuff.

All | AudioDrama | Chatter | Fun | Groovy | Java | Life

grash: a unix-like shell for your JVM
Posted on 25 Oct 2004
"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.

NOTES

How do I embed grash in my own programs

  • put grash.jar in your classpath
  • include these lines somewhere in your code (note: you can pass your own input/output streams in the Grash constructor}
import com.javanicus.grash.Grash;
...
Grash grash = new Grash(myObject);
Thread consoleThread = new Thread(grash);
consoleThread.start();

Under the covers

  • 100% Groovy source, at the moment (although some unit tests on Java will be needed)
  • Object references are currently piped around, rather than streams.
  • The grash command line parser takes precedence over groovy, so any ; | > & symbol will be ignored by the time that groovy sees it.

What's left to do...

  • Everything...
  • I have implemented other features such as cd , ps , pipes, redirects and some advanced options on ls
    • Feel free to play with them.
  • Decisions over pipes:
    • object handles or serialized objects ?
    • Do the methods in the subsequent pipe commands call methods on the result of the former, or methods on the 'current working instance' ?

HEALTH WARNINGS

  • This project is just a baby (born 14 Oct 2004), and is an extrapolation from the concept of file/object equivalence.
  • This project uses Groovy which is still in Beta.
  • grash.jar contains all of groovy-all.jar, to help ease of setup. However a version without included groovy will be available upon request.
  • You won't be able to get STDIN (InputStream) to work if you are running your program from ant and it's derivates.

BIG DISCLAIMERS

  • "It's amazing what you can do when you're too stupid to realised that it is impossible to achieve"
  • I have no clue whether this will be useful, I'm just experimenting right now.

I'm looking forward to your comments and ideas

25 Oct 2004 |

 

 
October 2004
SunMonTueWedThuFriSat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Prev | Today | Next

rss:subscribe (All)



What I'm reading
my feed aggregator ->box

My websites
London Java Meetups
Programming Projects
Elite in Java
megg
Blogmento
Jez's Photos
Fantasy Stock Market
Cool Saxophonist
Doctor Who Audios
Pisces Audios

Other Blogs
Mike Cannon-Brookes
James Strachan
Joe Walnes
Sam Dalton
Simon Brown
Cameron Purdy
Mike Roberts
Erik C. Thauvin
John Martin
Manfred Riem

B5 d++ t++ k s+ u- f
i+ o+ x-- e+ l- c--

powered by blogmento