« 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 98 : Ligne 98 :
The bashj script may be called as interpreter, or interactively for various actions. Use <span style="font-family:courier new,courier,monospace;">bashj -help</span> to&nbsp;see this:
The bashj script may be called as interpreter, or interactively for various actions. Use <span style="font-family:courier new,courier,monospace;">bashj -help</span> to&nbsp;see this:
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">[bashj] Possible interactive command arguments&nbsp;are:<br/> &nbsp;-help<br/> &nbsp;-install<br/> &nbsp;-uninstall<br/> &nbsp;-status&nbsp;: server status<br/> &nbsp;-stop&nbsp;: stops the server<br/> &nbsp;-restart&nbsp;: restart server<br/> &nbsp;-out&nbsp;: show server stdOut<br/> &nbsp;-err&nbsp;: show server stdErr</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+classes <filter>&nbsp;: lists classes accessible to bashj clients<br/> &nbsp;+methods <filter>&nbsp;: lists methods accessible to bashj clients<br/> &nbsp;+fields &nbsp;<filter>&nbsp;: lists fields &nbsp;accessible to bashj clients<br/> &nbsp;+status&nbsp;: server status<br/> &nbsp;+info&nbsp;: server line status<br/> &nbsp;+ex <example>&nbsp;: run and check example bashj script<br/> &nbsp;+eval <expression>&nbsp;: call the double eval method</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+pp <bashj file>&nbsp;: preprocessor output<br/> &nbsp;+PP <bashj file>&nbsp;: preprocessor analysis</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+uDoc&nbsp;: view documentation of u methods libraries</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+ <call() expression(s)>&nbsp;: verbosely evaluate java call(argument)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; example: &nbsp;bashj + "Math.hypot(3.0,4.0)" "Math.PI" "u.classes()"&nbsp;</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;<simple function expression>&nbsp;: evaluates java call(argument)<br/> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;example:&nbsp; &nbsp; bashj "Math.hypot(3.0,4.0)"</span></div>
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">[bashj] Possible interactive command arguments&nbsp;are:<br/> &nbsp;-help<br/> &nbsp;-install<br/> &nbsp;-uninstall<br/> &nbsp;-status&nbsp;: server status<br/> &nbsp;-stop&nbsp;: stops the server<br/> &nbsp;-restart&nbsp;: restart server<br/> &nbsp;-out&nbsp;: show server stdOut<br/> &nbsp;-err&nbsp;: show server stdErr</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+classes <filter>&nbsp;: lists classes accessible to bashj clients<br/> &nbsp;+methods <filter>&nbsp;: lists methods accessible to bashj clients<br/> &nbsp;+fields &nbsp;<filter>&nbsp;: lists fields &nbsp;accessible to bashj clients<br/> &nbsp;+status&nbsp;: server status<br/> &nbsp;+info&nbsp;: server line status<br/> &nbsp;+ex <example>&nbsp;: run and check example bashj script<br/> &nbsp;+eval <expression>&nbsp;: call the double eval method</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+pp <bashj file>&nbsp;: preprocessor output<br/> &nbsp;+PP <bashj file>&nbsp;: preprocessor analysis</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+uDoc&nbsp;: view documentation of u methods libraries</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;+ <call() expression(s)>&nbsp;: verbosely evaluate java call(argument)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; example: &nbsp;bashj + "Math.hypot(3.0,4.0)" "Math.PI" "u.classes()"&nbsp;</span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">&nbsp;<simple function expression>&nbsp;: evaluates java call(argument)<br/> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;example:&nbsp; &nbsp; bashj "Math.hypot(3.0,4.0)"</span></div>

=== Installed files ===
=== Installed files ===


Ligne 104 : Ligne 105 :
*'''<span style="font-family:courier new,courier,monospace;">bashjInstall</span>'''<span style="font-family:courier new,courier,monospace;"><version></span>'''<span style="font-family:courier new,courier,monospace;">.tar</span>''' (a tar file containing all bashj required files)
*'''<span style="font-family:courier new,courier,monospace;">bashjInstall</span>'''<span style="font-family:courier new,courier,monospace;"><version></span>'''<span style="font-family:courier new,courier,monospace;">.tar</span>''' (a tar file containing all bashj required files)
*'''<span style="font-family:courier new,courier,monospace;">bashj</span>'''&nbsp;(the central&nbsp;script, including&nbsp;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;(the central&nbsp;script, including&nbsp;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.install</span>'''&nbsp;(installation&nbsp;script)
*'''<span style="font-family:courier new,courier,monospace;">bashj.wrk</span>'''&nbsp;(auxiliary script)
*'''<span style="font-family:courier new,courier,monospace;">bashj.wrk</span>'''&nbsp;(auxiliary script)
*'''<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;">bashjServer.jar</span>''' (a jar containing the&nbsp;compiled java class <span style="font-family:courier new,courier,monospace;">'''bashj.server.class'''</span>)
Ligne 109 : Ligne 111 :
*<span style="font-family:courier new,courier,monospace;">'''example/'''</span>&nbsp;(a subdirectory with various example scripts (executable files))
*<span style="font-family:courier new,courier,monospace;">'''example/'''</span>&nbsp;(a subdirectory with various example scripts (executable files))
*<span style="font-family:courier new,courier,monospace;">'''jarlib/'''</span>&nbsp;(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;">'''jarlib/'''</span>&nbsp;(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;">'''maps/'''</span>&nbsp;(a subdirectory with the persistent maps)


Working transient files are dynamically created in <span style="font-family:courier new,courier,monospace;">'''/dev/shm/bashj'''</span>, with the following subdirectories
Working transient files are dynamically created in <span style="font-family:courier new,courier,monospace;">'''/dev/shm/bashj'''</span>, with the following subdirectories


*'''<span style="font-family:courier new,courier,monospace;">classes/</span>'''&nbsp;The dynamically created java classes
*'''<span style="font-family:courier new,courier,monospace;">classes/</span>'''&nbsp;The dynamically created java classes
*<span style="font-family:courier new,courier,monospace;">'''srv/'''</span> server related data (pid, stdout, stderr)
*<span style="font-family:courier new,courier,monospace;">'''srv/'''</span> server related data (pid, stdout, stderr)


=== Speed notes ===
=== Speed notes ===

Version du 27 juin 2018 à 08:34

What ?                   

The bashj project is hosted on sourceForgebashj is an extended bash allowing to use java libraries, methods and source code from bash scripts.

The bashj code combines

  • java performance and readability
  • bash power and versatility

-> Current version 0.997 (june 2018) is still a beta release... Release 1.0 expected july 2018 !

This page is an introduction to bashj, with general notes and installation guide.

There is also a bashj programming guide page.

To bash developers, bashj brings

  • Readability in the java section (OO style coding)
  • JVM efficiency for CPU intensive components
  • modular extensibility using java packages
  • floating point (double) values variable and evaluation functions
  • Math tools
  • Swing UI tools
  • Inter process tools between multiple bash process (and java process)
  • Host registry
  • ...

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 ... In fact I was frequently disappointed by the incapacity to reuse java results and expertise within scripts (this was only possible with heavy java process launching, and main() entry points). I was also frustrated by the strange and numerous coding conventions necessary to perform simple bash tasks. So I decided to set up and use this bashj tool.

Since bashj appeared to offer significant benefits, I decided to make it publicly available.

In terms of genetics, consider bashj as a bash mutant, whose genome has been loaded with wide portions of the java genome. 

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 ?

Bashj flows.png

A bashj server (a java background process)  communicates with bash scripts (considered as clients).

Dedicated functions are internally defined, and used from bash script clients to request server actions.

The communication uses mainly TCP connections.

This may be also be seen as a kind of bash preprocessor : bash itself is called by the bashj process.

Requirements and limitations

  • bash scripts only (version.4.*+)
  • Linux only (kernel 4.*+)
  • java JDK (9+) (JRE is not enough)
  • no interactive input (no stdin operation, like readline)
  • only public static methods (and public static fields) may be called 
  • only methods with primitive types (including String) as parameters and return value (neither array, nor collections). Varargs are supported by bashj.
  • no embedded invocations of java methods 
  • These greek characters φ χ ψ τ have specific usage within bashj and may not be used in bashj sources

Installation

Follow carefully these steps (commands in a terminal window)

download from sourceforge bashjInstall-<version>.tar and bashj.install
go to the directory containing the downloaded files
sudo chmod +x ./bashj.install
sudo ./bashj.install             # launch the installation script

For the uninstallation:

cd ; sudo /opt/bashj/bashj.install -uninstall 

Check, try and test

Various bashj scripts examples are in /var/lib/bashj/example/.Open a bash terminal window and type the following commands to discover bashj's elementary actions.

bashj -help
bashj "Math.cos(1.0)"               # check that the bashj interpreter operates
cd /var/lib/bashj/example/          # going to the example directory
ls                                  # view list of examples
cat javaLang                        # having a look at the code
./javaLang                          # running the code
cat check
./check
bashj +PP factorial                 # understanding how bashj works internally
./all                               # run all examples - shows execution speed

The same may be realized for all example scripts.

It is suggested to try, copy, adapt these examples scripts.

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 bashj interpreter.

They should be called as indicated in the programming guide..

Annexes

Support

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

bashj interactive commands

The bashj script may be called as interpreter, or interactively for various actions. Use bashj -help to see this:

[bashj] Possible interactive command arguments are:
 -help
 -install
 -uninstall
 -status : server status
 -stop : stops the server
 -restart : restart server
 -out : show server stdOut
 -err : show server stdErr
 +classes <filter> : lists classes accessible to bashj clients
 +methods <filter> : lists methods accessible to bashj clients
 +fields  <filter> : lists fields  accessible to bashj clients
 +status : server status
 +info : server line status
 +ex <example> : run and check example bashj script
 +eval <expression> : call the double eval method
 +pp <bashj file> : preprocessor output
 +PP <bashj file> : preprocessor analysis
 +uDoc : view documentation of u methods libraries
 + <call() expression(s)> : verbosely evaluate java call(argument)
            example:  bashj + "Math.hypot(3.0,4.0)" "Math.PI" "u.classes()" 
 <simple function expression> : evaluates java call(argument)
            example:    bashj "Math.hypot(3.0,4.0)"

Installed files

The files are installed under /opt/bashj/, and include:

  • bashjInstall<version>.tar (a tar file containing all bashj required files)
  • bashj (the central script, including the command interpreter - a link is created in /usr/bin)
  • bashj.install (installation script)
  • bashj.wrk (auxiliary script)
  • bashjServer.jar (a jar containing the compiled java class bashj.server.class)
  • README (a readme file - short intro to this wiki page)
  • example/ (a subdirectory with various example scripts (executable files))
  • jarlib/ (a subdirectory with the jars choosen by the user to make the involved classes available  to bashj)
  • maps/ (a subdirectory with the persistent maps)

Working transient files are dynamically created in /dev/shm/bashj, with the following subdirectories

  • classes/ The dynamically created java classes
  • srv/ server related data (pid, stdout, stderr)

Speed notes

The execution time of bashj  is excellent due to several factors:

  • TCP efficiency
  • in-memory dynamic java compiling
  • cache-based java class loading
  • JVM global efficiciency

Simple tests (on a 2018 average machine) indicate that a basic java method call requires around 1 msec. This probably depends on TCP config parameters.

The startup time for a bashj script hosting java methods is around 8 msec.

For a java call involving heavy CPU load, the execution delay for the call is the same as in a direct JVM.

For the minimal "Hello world" program, the minimal and median execution times are roughly:

  minimum (msec) median (msec)
bash 2 6
bashj 7 11
java 72 80

And this shows that in some cases, executing a java program from its main() entrypoint is faster with bashj than with java itself ! 

Future Improvements

  • ? Create a installable package for debian distribs.
  • ? Organize the bashj server as a Linux service