tags:

views:

139

answers:

6
  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

+1  A: 

Groovy allows scripting - and the syntax is close to Java

BZ
+1  A: 

I'm not exactly sure what you're looking for -- something like JShell? Something like Rhino (Javascript implementation in Java)? Something like Groovy (a dynamic language that produces code for the JVM)?

chryss
Something like JShell. But JShell is in such state that it would be basically the same for me if I start from scratch
Jaka
What's the use case?
chryss
Check the question description again, I have updated it.
Jaka
+1  A: 

You could try the Groovy Shell http://groovy.codehaus.org/Groovy+Shell

Or beanshell http://www.beanshell.org/

Both are dynamic languages that run in the jvm, and have a syntax close to java while letting you call any existing java classes.

crowne
+1  A: 
  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, ).

Gilles
+1 For the linkin with C implementation idea! Otherwise yes, I meant POSIX shell, thanks. I would not use existing scripts, but existing people who know how to do things in bash.I will leave this for a while and if nobody suggests a Java implementation, I will accept your answer
Jaka
And there is a Windows zsh port which does not depend on Cygwin. Yay!
Jaka
@Jaka: is there? all I know of is antiques. I see that [http://zsh-nt.sourceforge.net/](WinZsh) has been resurrected, but it's still a 10-year old version. Or did you find something else?
Gilles
@Gilles: I wrote the comment too quickly. I just saw "resurrected" and latest release dated 2010-02-16. Later I realized what was actually released :)
Jaka
+1  A: 

Check out this duscussion: http://www.antlr.org/pipermail/antlr-interest/2006-May/016235.html

I guess you don't need a full grammar. Define a subset of a language, experiment a little bit. Maybe you don't even need antlr and you can write a parser by hand. Or use scala, you can turn it into any language you need.

tulskiy
I know about the YACC Bash grammar. I have looked at it and it hurts my eyes. :)It seems that nobody made YACC to ANTLR translator yet. It is probably also not easy since ANTLR is LL(*) and LALR.
Jaka
+1  A: 

You can try using the Apache Commons CLI library that has implementations for parsing POSIX, GNU like command line arguments.

Rahul