« Project bashj : a bash mutant with java support » : différence entre les versions

De Lillois Fractale Wiki
Aller à la navigation Aller à la recherche
Contenu ajouté Contenu supprimé
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 5 : Ligne 5 :


'''bashj''' is an extended bash allowing to use java libraries, functions and code from bash scripts.
'''bashj''' is an extended bash allowing to use java libraries, functions and code from bash scripts.

'''Java Shell Bridge''' (jsb) is the underlying technology used by bashj. It is mainly a server executing java code for the benefit of a bash session (interactive or scripted).


<span style="color:#008000;"><cite>version 0.981&nbsp;- june 2018 - this is still a beta release...</cite></span>
<span style="color:#008000;"><cite>version 0.981&nbsp;- june 2018 - this is still a beta release...</cite></span>
Ligne 24 : Ligne 22 :
The jsbServer (a java daemon process)&nbsp; communicates with bash scripts (considered as clients).
The jsbServer (a java daemon process)&nbsp; communicates with bash scripts (considered as clients).


Specialized bash function (jsb() and jsbXXX()) are defined. They are used from bash script clients to request server actions.
A dedicated function (<span style="font-family:courier new,courier,monospace;">jsb()</span>) is&nbsp;internally defined. It is&nbsp;used from bash script clients to request server actions.


The communication uses&nbsp;two&nbsp;named&nbsp;pipes.
The communication uses&nbsp;two&nbsp;named&nbsp;pipes.
Ligne 32 : Ligne 30 :
== Limitations ==
== Limitations ==


*bash only
*bash scripts only (not usable interactively)
*Linux only
*Linux only
*java 9+
*java 9+
Ligne 53 : Ligne 51 :
For the uninstallation:
For the uninstallation:


#run <span style="font-family:courier new,courier,monospace;">. jsbInit</span>
#run bashj<span style="font-family:courier new,courier,monospace;">Uninstall</span>&nbsp;
#run <span style="font-family:courier new,courier,monospace;">jsbUninstall</span>&nbsp;


== Check, try and test ==
== Check, try and test ==
Ligne 68 : Ligne 65 :
The bashj interpreter only works for&nbsp;scripts. It may not be used for an interactive session.
The bashj interpreter only works for&nbsp;scripts. It may not be used for an interactive session.


A bashj&nbsp;file is similar&nbsp;to a standard bash file. Just replace the shebang&nbsp;: <span style="font-family:courier new,courier,monospace;">'''#!/bin/bashj'''</span>&nbsp;instead of <span style="font-family:courier new,courier,monospace;">'''#!/bin/bash'''</span>.
A bashj&nbsp;file is similar&nbsp;to a standard bash file. Just replace the shebang&nbsp;: <span style="font-family:courier new,courier,monospace;">'''#!/usr/bin/bashj'''</span>&nbsp;instead of <span style="font-family:courier new,courier,monospace;">'''#!/bin/bash'''</span>.


Three families of java methods & fields may be called
Three families of java methods & fields may be called


*Those defined in the beginning of the running bash script itself, between a line containing&nbsp;<span style="font-family:courier new,courier,monospace;">'''#@javaStart'''</span>and a line containing<span style="font-family:courier new,courier,monospace;">'''&nbsp;#@javaEnd'''</span>. Java methods are defined there without package name and without class name. The implicit method name is "'''j'''". So a function&nbsp;public static <span style="font-family:courier new,courier,monospace;">int factorial(int n)</span> may be called in the script body as&nbsp;'''j.factorial(int)'''.
*Those defined in the beginning of the running bash script itself, between a line containing&nbsp;<span style="font-family:courier new,courier,monospace;">'''#@java&nbsp;'''</span>and a line containing<span style="font-family:courier new,courier,monospace;">'''&nbsp;#@bash'''</span>. Java methods are defined there without package name and without class name. The implicit class name is "'''j'''". So a function&nbsp;public static <span style="font-family:courier new,courier,monospace;">int factorial(int n)</span> may be called in the script body as&nbsp;'''j.factorial(int)'''.
*Those present in the standard java packages&nbsp;'''java.lang.System'''&nbsp;&nbsp;and '''java.lang.Math'''&nbsp; (possibly more later if requested).&nbsp;
*Those present in the standard java packages&nbsp;'''java.lang.System'''&nbsp;&nbsp;and '''java.lang.Math'''&nbsp; (possibly more later if requested).&nbsp;
*Those present in jars put by the user in the subdirectory <span style="font-family:courier new,courier,monospace;">jarlib</span>.
*Those present in jars put by the user in the subdirectory <span style="font-family:courier new,courier,monospace;">jarlib</span>.
Ligne 111 : Ligne 108 :


*'''<span style="font-family:courier new,courier,monospace;">bashjInstall.jar</span>''' (a jar containing all bashj required files)
*'''<span style="font-family:courier new,courier,monospace;">bashjInstall.jar</span>''' (a jar containing all bashj required files)
*'''<span style="font-family:courier new,courier,monospace;">bashjInstall</span>'''&nbsp;(an&nbsp;installation script)
*'''<span style="font-family:courier new,courier,monospace;">bashjInstall</span>'''&nbsp;(an&nbsp;installation script) & <span style="font-family:courier new,courier,monospace;">'''bashUninstall'''</span>
*'''<span style="font-family:courier new,courier,monospace;">jsbServer.jar</span>''' (a jar containing the&nbsp;compiled java class <span style="font-family:courier new,courier,monospace;">'''jsbServer.class'''</span>)
*'''<span style="font-family:courier new,courier,monospace;">bashjServer.jar</span>''' (a jar containing the&nbsp;compiled java class <span style="font-family:courier new,courier,monospace;">'''bashj.server.class'''</span>)
*'''<span style="font-family:courier new,courier,monospace;">jsbInit</span>''' (a set of bash functions) (a link is created in <span style="font-family:courier new,courier,monospace;">/usr/bin</span>)
*'''<span style="font-family:courier new,courier,monospace;">bashj</span>'''&nbsp;(a script, the command interpreter) (a link is created in <span style="font-family:courier new,courier,monospace;">/usr/bin</span>)
*'''<span style="font-family:courier new,courier,monospace;">bashj</span>'''&nbsp;(a script, the command interpreter) (a link is created in <span style="font-family:courier new,courier,monospace;">/usr/bin</span>)
*'''<span style="font-family:courier new,courier,monospace;">README</span>''' a readme file (a short version of this page)
*'''<span style="font-family:courier new,courier,monospace;">README</span>''' a readme file (a short intro to this page)
*<span style="font-family:courier new,courier,monospace;">'''pipe/'''</span>a&nbsp;subdirectory used for named pipes
*<span style="font-family:courier new,courier,monospace;">'''pipe/'''</span>a&nbsp;subdirectory used for named pipes
*'''<span style="font-family:courier new,courier,monospace;">classes/</span>''' a subdirectory with transient compiled java classes
*'''<span style="font-family:courier new,courier,monospace;">classes/</span>''' a subdirectory with transient compiled java classes
*<span style="font-family:courier new,courier,monospace;">'''jarlib/'''</span> a subdirectory with the jars choosen by the user to make the involved classes available&nbsp;&nbsp;&nbsp;
*<span style="font-family:courier new,courier,monospace;">'''jarlib/'''</span> a subdirectory with the jars choosen by the user to make the involved classes available&nbsp; to bashj
*<span style="font-family:courier new,courier,monospace;">'''example/'''</span> a subdirectory with various example scripts (executable files)
*<span style="font-family:courier new,courier,monospace;">'''example/'''</span> a subdirectory with various example scripts (executable files)

Version du 12 juin 2018 à 07:32

What ?

The bashj project is hosted on sourceForge.

bashj is an extended bash allowing to use java libraries, functions and code from bash scripts.

version 0.981 - june 2018 - this is still a beta release...

Why ?

Java and bash are often partners in software projects.

Personnally I used both of them widely. Being rather lazy, I tried to minimize the number of necessary techonologies in my projects. These two covered 99% of my needs together. Java is perfect to translate concepts into software. But bash is necessary for various OS level tasks, like process control, deployment ... But I was frequently disappointed by the incapacity to reuse java results and expertise from scripts (this was only possible with java process launching, and main() entry points). So I decided to set up the tool described here... 

Their possible interactions are numerous but limited in terms of integration.

With this project, any script is given the capacity to quickly and directly call java methods without creating new OS processes.

How ?

The jsbServer (a java daemon process)  communicates with bash scripts (considered as clients).

A dedicated function (jsb()) is internally defined. It is used from bash script clients to request server actions.

The communication uses two named pipes.

All this may be also be seen as a bash preprocessor.

Limitations

  • bash scripts only (not usable interactively)
  • Linux only
  • java 9+
  • requires javac (JDK installed)
  • only public static methods may be called 
  • methods with primitive types (including String) as parameters and return value
  • no varargs

Installation

Follow carefully these steps:

  1. create a directory  :  sudo mkdir -m=777  /var/lib/bashj/
  2. download from  sourceForge the bashjInstall.jar installation file and move it in this directory
  3. go to the installation directory : cd /var/lib/bashj/
  4. run in bash : jar xvf bashjInstall.jar
  5. chmod +x ./bashjInstall
  6. run in bash : sudo ./bashjInstall

For the uninstallation:

  1. run bashjUninstall 

Check, try and test

Various bashj scripts examples are in /var/lib/bashj/example/.

cd /var/lib/bashj/example/   # going to the example directory
cat ex.1                     # having a look at the code
./ex.1                       # running the code

The same may be realized for various ex.? scripts.

It is suggested to try, copy, adapt some examples scripts present in the example subdiretory

bashj syntax rules and java calls

The bashj interpreter only works for scripts. It may not be used for an interactive session.

A bashj file is similar to a standard bash file. Just replace the shebang : #!/usr/bin/bashj instead of #!/bin/bash.

Three families of java methods & fields may be called

  • Those defined in the beginning of the running bash script itself, between a line containing #@java and a line containing #@bash. Java methods are defined there without package name and without class name. The implicit class name is "j". So a function public static int factorial(int n) may be called in the script body as j.factorial(int).
  • Those present in the standard java packages java.lang.System  and java.lang.Math  (possibly more later if requested). 
  • Those present in jars put by the user in the subdirectory jarlib.

Methods may be called with various equivalent syntax. The following lines (in a bashj script) provide the same result :

  • echo Math.hypot(3.0,4.0)
  • echo java.lang.Math.hypot(3.0,4.0)
  • echo $(jsb Math.hypot 3.0 4.0)

The first one is the most readable and recommended.

Configuration

No configuration is necessary.

However it is possible for the user to put various jar files under the jarlib/ directory.

All public static methods and all public static fields defined in these classes will be available in the bridge.

They should be called as indicated before.

Improvements

  • ? Handle varargs argument.
  • ? Create a installable package for debian distribs.
  • ? Organize the jsbServer as a Linux service

Annexes

Support

Please contact fil@gonze.org for support, remarks, suggestions,...

Installed Files

The files are installed under /var/lib/bashj/, and include:

  • bashjInstall.jar (a jar containing all bashj required files)
  • bashjInstall (an installation script) & bashUninstall
  • bashjServer.jar (a jar containing the compiled java class bashj.server.class)
  • bashj (a script, the command interpreter) (a link is created in /usr/bin)
  • README a readme file (a short intro to this page)
  • pipe/a subdirectory used for named pipes
  • classes/ a subdirectory with transient compiled java classes
  • jarlib/ a subdirectory with the jars choosen by the user to make the involved classes available  to bashj
  • example/ a subdirectory with various example scripts (executable files)