« 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
 
(116 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :


== What ?                    ==
== What ? ==


The '''bashj''' project is hosted on [https://sourceforge.net/projects/bashj/ sourceForge].
The '''bashj''' project is hosted on [https://sourceforge.net/projects/bashj/ sourceForge]. '''bashj''' is an extended bash allowing to use java libraries, methods and source code from bash scripts.


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


*java '''performance '''and'''readability'''
Coding examples are visible at the end of this page.
*bash '''power '''and'''versatility'''


<span style="color:#008000;"><cite>-> Current version '''1.0'''&nbsp;(july&nbsp;2018)...</cite></span>
[[File:Bashj.png|left|100x100px|Bashj.png]]


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

<ul style="margin-left: 120px;">
There is also a '''[[Bashj_programming_guide|<span style="background-color:#E6E6FA;">bashj programming guide</span>]]''' page.
<li>java performance and readability</li>

<li>bash power and versatility</li>
To bash developers, bashj&nbsp;brings
</ul>


*'''Readability''' in the java section (OO style coding)
<span style="color:#008000;"><cite>version 0.982&nbsp;(june 2018)&nbsp;is still a beta release...</cite></span>
*JVM '''efficiency''' for CPU intensive components
*modular '''extensibility''' using java packages
*'''floating point''' (double)&nbsp;values variable and evaluation functions
*'''Math''' tools
*'''Swing''' UI tools
*'''Inter process'''&nbsp;tools between multiple bash process (and java process)
*Host '''registry'''
*...


== Why&nbsp;? ==
== Why&nbsp;? ==
Ligne 22 : Ligne 31 :
Java and bash are often partners in software projects.
Java and bash are often partners in software projects.
<blockquote>
<blockquote>
<cite>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 within scripts (this was only possible with heavy java process launching, and main() entry points). So I decided to set up the tool described here...&nbsp;</cite>
<cite>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.</cite>

<cite>Since bashj appeared to offer significant benefits, I decided to make it publicly available.</cite>

<cite>In terms of genetics, consider bashj as a bash mutant, whose genome has been loaded with wide portions of the java genome.&nbsp;</cite>
</blockquote>
</blockquote>
Their possible interactions are numerous but limited in terms of integration.
Their possible interactions are numerous but limited in terms of integration.
Ligne 30 : Ligne 43 :
== How&nbsp;? ==
== How&nbsp;? ==


[[File:Bashj flows.png|800x450px|Bashj flows.png]]
A bashj server (a java daemon process)&nbsp; communicates with bash scripts (considered as clients).


A bashj server (a java background process)&nbsp; communicates with bash scripts (considered as clients).
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.


Dedicated functions are&nbsp;internally defined, and&nbsp;used from bash script clients to request server actions.
The communication uses&nbsp;two&nbsp;named&nbsp;pipes.


The communication uses&nbsp;mainly TCP connections.
All this may be also be seen as a kind of bash ''preprocessor''.


This may be also be seen as a kind of bash ''preprocessor''&nbsp;: bash itself is finally called by the bashj process.
== Limitations ==


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


*'''bash'''&nbsp;(version 4.*+)
== Installation ==
*'''Linux'''&nbsp; (kernel version 3.*+)
*'''java''' '''JDK''' (version 9+) (JRE is not enough)
*'''64 bits''' (because Oracle dropped 32-bits support for java from version 9)
*only public static java methods (and public static fields) may be called&nbsp;
*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&nbsp;
*These greek characters ''<span style="font-size:larger;">φ&nbsp;χ&nbsp;ψ&nbsp;τ</span>&nbsp;''have specific usage within bashj and may not be used in bashj sources


bashj is developped and tested on Linux (Ubuntu 18.04).&nbsp;
Follow carefully these steps:


It should&nbsp;run (without having been tested) ''at least'' on '''64-bits&nbsp;debian-based linux systems'''&nbsp; with the components listed above (linux kernel, bash &&nbsp;java).
#create a directory&nbsp;&nbsp;:&nbsp; <span style="font-family:courier new,courier,monospace;">sudo</span>&nbsp;<span style="font-family:courier new,courier,monospace;">mkdir -m=777&nbsp;</span>&nbsp;<span style="font-family:courier new,courier,monospace;">'''/var/lib/bashj/'''</span>
#download from&nbsp;&nbsp;[https://sourceforge.net/projects/bashj/ sourceForge]&nbsp;the <span style="font-family:courier new,courier,monospace;">bashjInstall.jar</span> installation file and move it in this directory
#running a bash session, go to the installation directory&nbsp;: <span style="font-family:courier new,courier,monospace;">cd</span>&nbsp;<span style="font-family:courier new,courier,monospace;">/var/lib/bashj/</span>
#run &nbsp;:&nbsp;<span style="font-family:courier new,courier,monospace;">jar xvf bashjInstall.jar</span>
#<span style="font-family:arial,helvetica,sans-serif;">run</span><span style="font-family:courier new,courier,monospace;"><span style="font-family:arial,helvetica,sans-serif;">: </span>chmod +x&nbsp;bashjInstall</span>
#run&nbsp;&nbsp;: <span style="font-family:courier new,courier,monospace;">sudo ./bashjInstall</span>


== Installation ==
For the uninstallation:

#run&nbsp;: <span style="font-family:courier new,courier,monospace;">sudo /var/lib/bashj/bashjUninstall&nbsp;</span>


The target installation directory is <span style="font-family:courier new,courier,monospace;">/opt/bashj&nbsp;</span>. Follow carefully these steps (commands in a terminal window)
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;">''download from sourceforge''&nbsp;<span style="font-family:courier new,courier,monospace;">'''bashjInstall-<''version''>.tar''' </span>''<span style="font-family:arial,helvetica,sans-serif;">and</span>''<span style="font-family:courier new,courier,monospace;">&nbsp;'''bashj.install'''</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;">''<span style="font-family:arial,helvetica,sans-serif;">go to the directory containing the downloaded files, and&nbsp;:</span>''</div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">sudo chmod +x ./bashj.install</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">sudo ./bashj.install &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>''<span style="font-family:arial,helvetica,sans-serif;"># launching the installation script</span>''</div>
For the uninstallation:
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">cd</span>&nbsp;;&nbsp;<span style="font-family:courier new,courier,monospace;">sudo /opt/bashj/bashj.install -uninstall&nbsp;</span></div>
== Check, try and test ==
== Check, try and test ==


Various&nbsp;bashj scripts examples are in&nbsp;<span style="font-family:courier new,courier,monospace;">/var/lib/bashj/example/.</span>
Various&nbsp;bashj scripts examples are in&nbsp;<span style="font-family:courier new,courier,monospace;">/var/lib/bashj/example/.</span><span style="font-family:arial,helvetica,sans-serif;">Open a bash terminal window and type the following commands to discover bashj's elementary actions.</span>
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">cd&nbsp;/var/lib/bashj/example/&nbsp; &nbsp; # going to the example directory</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">ls&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # view list of examples</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">cat javaLang&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # having a look at the code</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">./javaLang&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # running the code</span></div>
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">bashj -help</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">bashj "Math.cos(1.0)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# check that the bashj interpreter operates</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">cd&nbsp;/var/lib/bashj/example/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # going to the example directory</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">ls&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # view list of examples</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">cat javaLang&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # having a look at the code</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">./javaLang&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # running the code</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">cat check</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">./check</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">bashj +PP factorial&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# understanding how bashj works internally</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">./all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# run all examples - shows execution speed</span></div>
The same may be realized for all example&nbsp;scripts.
The same may be realized for all example&nbsp;scripts.


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


== bashj&nbsp;syntax rules and java calls ==

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;">'''#!/usr/bin/bashj'''</span>&nbsp;instead of <span style="font-family:courier new,courier,monospace;">'''#!/bin/bash'''</span>.

Several 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;">'''#@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;
*The control methods provided by the bashj server:
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">server.info()</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">server.classes()</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">server.classes(String grep)</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">server.methods()</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">server.methods(String grep)</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">server.fields()</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">server.fields(String grep)</span></div>
*Those provided as utilities as part of bashj. They are accessible with a class name "'''u'''", for instance <span style="font-family:courier new,courier,monospace;">'''u.toUpperCase'''</span>(String) or <span style="font-family:courier new,courier,monospace;">'''yellow'''</span>(String). The list is available using '''server.methods("bashj.u")'''.
*Those present in jars put by the user in the subdirectory <span style="font-family:courier new,courier,monospace;">jarlib</span>.

Methods may&nbsp;be called with various equivalent syntax. The following lines (in a bashj script) provide the same result&nbsp;:
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">echo&nbsp;Math.hypot(3.0,4.0)</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">echo java.lang.Math.hypot(3.0,4.0)</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">echo $(jsb Math.hypot 3.0 4.0)</span></div>
The first one is the most readable and recommended.

The call produces a bash varaibale wich my be used in the usual ways:
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">echo&nbsp;Math.hypot(3.0,4.0)</span></div> <div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">HYPOT =&nbsp;Math.hypot(3.0,4.0)</span></div>
Void java methods should be called using this syntax:
<div style="background: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); padding: 5px 10px; margin-left: 40px;"><span style="font-family:courier new,courier,monospace;">: server.info()</span></div>
== Configuration ==
== Configuration ==


Ligne 102 : Ligne 90 :
All public static methods and all public static fields defined in these classes will be available in the bashj interpreter.
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 before.
They should be called as indicated in the programming guide..

== Improvements ==

*? Handle varargs argument.
*? Create a installable package for debian distribs.
*? Organize the bashj server as a Linux service


== Annexes ==
== Annexes ==
Ligne 116 : Ligne 98 :
Please contact [mailto:fil@gonze.org fil@gonze.org] for support, remarks, suggestions,...
Please contact [mailto:fil@gonze.org fil@gonze.org] for support, remarks, suggestions,...


=== Installed Files ===
=== bashj interactive commands ===


The files are installed under <span style="font-family:courier new,courier,monospace;">'''/var/lib/bashj/'''</span>, and include:
The bashj script may be called as interpreter, or interactively for various actions. Use&nbsp;&nbsp;<span style="font-family:courier new,courier,monospace;">bashj -help&nbsp;</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] - administrative taks - no server required<br/> &nbsp;bashj -help &nbsp; &nbsp;: show this<br/> &nbsp;bashj -status &nbsp;: server status<br/> &nbsp;bashj -stop &nbsp; &nbsp;: stops the server<br/> &nbsp;bashj -restart&nbsp;: (re-)starts the server</span>


*'''<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;">&nbsp;bashj -trace &nbsp;&nbsp;: show server trace<br/> &nbsp;bashj -out &nbsp; &nbsp;&nbsp;: check server stdOut (normally empty)<br/> &nbsp;bashj -err &nbsp; &nbsp;&nbsp;: check server stdErr (normally empty)<br/> &nbsp;bashj -Trace &nbsp;&nbsp;: follow server trace</span>

*'''<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;">&nbsp;. bashj = &nbsp; &nbsp; &nbsp;: enable interactive java calls from bash session<br/> &nbsp; &nbsp;(type again 'bashj -help' for more help when session is enabled)</span>

<span style="font-family:courier new,courier,monospace;">[bashj] - interacting with server (launched if necessary)<br/> &nbsp;bashj +classes <filter> &nbsp;: lists classes accessible to bashj clients<br/> &nbsp;bashj +methods <filter> &nbsp;: lists methods accessible to bashj clients<br/> &nbsp;bashj +fields &nbsp;<filter> &nbsp;: lists fields &nbsp;accessible to bashj clients<br/> &nbsp;bashj +status &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: server status<br/> &nbsp;bashj +info &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: server line status<br/> &nbsp;bashj +pp <bashj file> &nbsp;&nbsp;: preprocessor output<br/> &nbsp;bashj +PP <bashj file> &nbsp;&nbsp;: preprocessor analysis<br/> &nbsp;bashj +ex <example> &nbsp; &nbsp; &nbsp;: run and check example bashj script<br/> &nbsp;bashj +eval <expression>&nbsp;: call the double eval method<br/> &nbsp;bashj +uDoc &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: list utility methods from class u (bashj.u)</span>

<span style="font-family:courier new,courier,monospace;">&nbsp;bashj + <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;<br/> &nbsp;bashj <call() expression(s)>&nbsp;: evaluate java call(argument)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; example: &nbsp;bashj "Math.hypot(3.0,4.0)" "Math.PI" "u.classes()"</span>
</div>
=== bashj interpreter mode ===

During a normal interactive bash session, it is possible to enable java calls provided by bashj. To enable the java calls , type this command:
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">. bashj =</span></div>
And from there, the user is allowed to mix bash commands with ''j-prefixed'' commands including java calls.
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace;">[bashj] bash interpreter with interactive java calls enabled<br/> &nbsp;enter simple bash commands, like 'ls -l' , 'ps' ...<br/> &nbsp;enter j-prefixed bash(j) commands, like&nbsp;:<br/> &nbsp; &nbsp; &nbsp; &nbsp;j echo "cos[3]=Math.cos(3)"<br/> &nbsp; &nbsp; &nbsp; &nbsp;j HYPOT="Math.hypot(3,4)"</span></div>
In interpreter mode, the'''''j'''''operates as a prefix. The command following this prefix (it is actually a bash function) is processed as if it was included in a bashj script.

=== Installed files ===

The files are installed under <span style="font-family:courier new,courier,monospace;">'''/opt/bashj/'''</span>, and include:

*'''<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.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;">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>)
*'''<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>'''&nbsp;(a readme file - short intro to this wiki 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;">'''example/'''</span>&nbsp;(a subdirectory with various example scripts (executable files))
*<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;">'''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;">classes/</span>''' a subdirectory with transient compiled java classes
*<span style="font-family:courier new,courier,monospace;">'''maps/'''</span>&nbsp;(a subdirectory with the persistent maps)&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)
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;">'''srv/'''</span> server related data (pid, stdout, stderr)

=== Speed notes ===

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

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

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

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

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

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

{| border="1" cellpadding="1" cellspacing="1" style="width: 500px;"
|-
! scope="row" | &nbsp;
! scope="col" style="background-color: rgb(204, 255, 255);" | minimum (msec)
! scope="col" style="background-color: rgb(204, 255, 255);" | median (msec)
|-
! scope="row" style="background-color: rgb(255, 204, 204);" | bash
| style="text-align: center;" | 2
| style="text-align: center;" | 6
|-
! scope="row" style="background-color: rgb(255, 204, 204);" | bashj
| style="text-align: center;" | 7
| style="text-align: center;" | 11
|-
! scope="row" style="background-color: rgb(255, 204, 204);" | java
| style="text-align: center;" | 72
| style="text-align: center;" | 80
|}

And this shows that in some cases, executing a simple java program from its&nbsp;<span style="font-family:courier new,courier,monospace;">main()</span>&nbsp;entrypoint is faster with bashj than with java itself&nbsp;!&nbsp;

=== Future Improvements ===


*''? Create an installable package for debian distribs''
=== Script coding&nbsp;examples ===
*''? Organize the bashj server as a Linux service''
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#!/usr/bin/bashj</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#@java</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>public static int factorial(int n)&nbsp;<br/> {if (n<=0) return(0);<br/> &nbsp;if (n==1) return(1);<br/> &nbsp;return(n*factorial(n-1));}</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#@bash</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo j.factorial(10)</code></span></div>
&nbsp;
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#!/usr/bin/bashj</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#@java<br/> public static void hello()<br/> {u.p("Hello, bashj&nbsp; world&nbsp;!");<br/> &nbsp;u.p("This code is executed in a JVM "+System.getProperty("java.version"));<br/> &nbsp;u.p("But it is processed through bashj and finally bash");}<br/> #@bash<br/> &nbsp;: j.hello()</code></span></div>
&nbsp;
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#!/usr/bin/bashj</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo Math.cos(0.5)<br/> echo Math.hypot(3.0,4.0)<br/> echo System.getProperty("java.runtime.version")</code></span></div>
&nbsp;
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#!/usr/bin/bashj</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#@java</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>public static String ax2_bx_c(double a,double b,double c)<br/> {final double r=b*b-4*a*c;<br/> &nbsp;if (r<0.0) return("NoRoot");<br/> &nbsp;if (r==0.0) return(""+(-b/(a+a)));<br/> &nbsp;final double sq=Math.sqrt(r);<br/> &nbsp;return(""+(-b-sq)/(a+a)+" "+(-b+sq)/(a+a));}</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#@bash</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo "... Delivers the roots of a quadratic equation"</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>A=1.0<br/> B=-5.0<br/> C=6.0<br/> echo "... Solving $A X^2 + $B X + $C"</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo "... Roots are " j.ax2_bx_c($A,$B,$C)</code></span></div>
&nbsp;
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#!/usr/bin/bashj</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo "bashj server utilities"<br/> echo<br/> &nbsp;: server.classes()<br/> echo<br/> &nbsp;: server.methods("bashj.u.")<br/> echo<br/> &nbsp;: server.fields("ersion")<br/> echo<br/> &nbsp;: server.info()</code></span></div>
&nbsp;
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>#!/usr/bin/bashj</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo "bashj string utilities"</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>WORD=Bonjour</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo length of $WORD is u.strlen($WORD)</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>LETTER=u</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo index of $LETTER in $WORD is u.indexOf($WORD,$LETTER)</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo Uppercase u.toUpperCase($WORD)</code></span></div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:small;"><code>echo Lowercase u.toLowerCase($WORD)</code></span></div>

Dernière version du 12 juillet 2018 à 13:06

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 andreadability
  • bash power andversatility

-> Current version 1.0 (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 finally called by the bashj process.

Requirements and limitations

  • bash (version 4.*+)
  • Linux  (kernel version 3.*+)
  • java JDK (version 9+) (JRE is not enough)
  • 64 bits (because Oracle dropped 32-bits support for java from version 9)
  • only public static java 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

bashj is developped and tested on Linux (Ubuntu 18.04). 

It should run (without having been tested) at least on 64-bits debian-based linux systems  with the components listed above (linux kernel, bash & java).

Installation

The target installation directory is /opt/bashj . 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, and :
sudo chmod +x ./bashj.install
sudo ./bashj.install             # launching 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] - administrative taks - no server required
 bashj -help    : show this
 bashj -status  : server status
 bashj -stop    : stops the server
 bashj -restart : (re-)starts the server

 bashj -trace   : show server trace
 bashj -out     : check server stdOut (normally empty)
 bashj -err     : check server stdErr (normally empty)
 bashj -Trace   : follow server trace

 . bashj =      : enable interactive java calls from bash session
   (type again 'bashj -help' for more help when session is enabled)

[bashj] - interacting with server (launched if necessary)
 bashj +classes <filter>  : lists classes accessible to bashj clients
 bashj +methods <filter>  : lists methods accessible to bashj clients
 bashj +fields  <filter>  : lists fields  accessible to bashj clients
 bashj +status            : server status
 bashj +info              : server line status
 bashj +pp <bashj file>   : preprocessor output
 bashj +PP <bashj file>   : preprocessor analysis
 bashj +ex <example>      : run and check example bashj script
 bashj +eval <expression> : call the double eval method
 bashj +uDoc              : list utility methods from class u (bashj.u)

 bashj + <call() expression(s)> : verbosely evaluate java call(argument)
            example:  bashj + "Math.hypot(3.0,4.0)" "Math.PI" "u.classes()" 
 bashj <call() expression(s)> : evaluate java call(argument)
            example:  bashj "Math.hypot(3.0,4.0)" "Math.PI" "u.classes()"

bashj interpreter mode

During a normal interactive bash session, it is possible to enable java calls provided by bashj. To enable the java calls , type this command:

. bashj =

And from there, the user is allowed to mix bash commands with j-prefixed commands including java calls.

[bashj] bash interpreter with interactive java calls enabled
 enter simple bash commands, like 'ls -l' , 'ps' ...
 enter j-prefixed bash(j) commands, like :
       j echo "cos[3]=Math.cos(3)"
       j HYPOT="Math.hypot(3,4)"

In interpreter mode, thejoperates as a prefix. The command following this prefix (it is actually a bash function) is processed as if it was included in a bashj script.

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 simple java program from its main() entrypoint is faster with bashj than with java itself ! 

Future Improvements

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