Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX shell like implementation in Java

Tags:

java

bash

grammar

  1. Does anybody know of an implementation of POSIX shell like language for scripting things in Java?

  2. If this is not available, does anybody know if there is a ANTLR or JavaCC grammar available somewhere which I might have missed?

edit: I know that I have Jython, JRuby, Groovy, JavaScript available for scripting, but none of them have bash like syntax.

This would not be used for scripting together Java code, but to allow people to run predefined commands which would manipulate with a big third party Media Asset Management system.

I would like to run things like:

ls | grep "something" > output

Where ls and grep would be Java commands. (This is just for illustrative purposes)

Thanks

like image 231
Jaka Avatar asked Aug 03 '10 17:08

Jaka


2 Answers

  1. All the POSIX shell implementations I know of are written in C, but I haven't particularly researched the question.

  2. POSIX shell (I assume that's what you mean by bash-like) syntax is fairly complex. There is no clear separation between lexing and parsing. On the other hand the parsing hardly requires any backtracking. So a parser generator might not help so much.

EDIT since you've clarified you want shell syntax:

I think your best bet is to use an existing shell. Here are a few architecture considerations:

  • You can just link your application into an existing shell. Add built-ins that manipulate your asset management system. There may be licensing issues. This gives one application instance per shell instance.

  • You can use a simple client-server architecture, where the server is part of the application and just responds to simple commands with no control logic, and the client is linked into the shell and doesn't access application data directly. Several shells (bash, ksh, zsh) already have means for TCP access.

    You might not need to reinvent a communication protocol; consider going over HTTP(S), for which server and client implementations are readily available. In fact you might even get away with having only scripts around wget or curl on the client side, so wouldn't need to patch the shell at all (this would make keeping complex state on the client side).

If you need to patch the shell (say, to add builtins), consider zsh, which has a module system. Your application (or the client part) would appear as a module that defines builtins and whatever else you need (for example the zsh distribution includes modules for things like ftp and mmap, ).

like image 56
Gilles 'SO- stop being evil' Avatar answered Oct 07 '22 00:10

Gilles 'SO- stop being evil'


Groovy allows scripting - and the syntax is close to Java

like image 34
BZ. Avatar answered Oct 07 '22 02:10

BZ.