Example Script Scripting
DESCRIPTION
The JANOS scripting language can be used in the batch environment. Here the
script renders commands which are then executed. This is similar to its use
in the WebServer situation where PHP renders the HTML page which then is
served. The batch script renders commands which are then executed. One
difference being that as each command is created it is executed. A complete
batch file is not rendered and then run. This allows script to respond to
the results of a previous command.
EXAMPLE
Batch files have the ability to masquerade as console commands. Here a
script creates a CKSUMS command which reports general message digest and
checksum information for requested files. For example:
bruce_dev /> cksums jniorsys.log /flash/cksums.bat
file: /jniorsys.log
date: 1624647433 2021-06-25 18:57:13 UTC
crc: f76beec3
md4: 51b9e5115b62af92df900ee7e66b4d68
md5: 6c958d3dce3edc8ef9a44e380030419b
sha1: 1b962afcdae46e666407cd64a97ca772d4ee9a8d
sha256: 69ccdf8f3236d976e244c15994e80271eee1ff2ba72ce0f71268d51fa7357361
file: /flash/cksums.bat
date: 1614090556 2021-02-23 14:29:16 UTC
crc: 5a9ae151
md4: 2869eedfa73a81d63c99fe60899b2f87
md5: ef3121cafc74a6ffbb179806d4c7dcef
sha1: 71fa94ec88fceaea208a2c284f16f7d59911e9ac
sha256: 44d4e35d87148c63acbdf408d4c94cbcd862d106c5d502ea6da6ba1d22535d17
bruce_dev />
Here we request digests for the system log and the script itself. Note that
the last modified date for the file is also reported. This can be very
useful in checking file validity by comparing these against digests calculated
on the original file by the source.
In considering the script needed to perform this we first see that command
line parameters are possible and our script needs to process 1 or more
as necessary. In fact the script allows wildcards and can report for all
matching files. Secondly the script is executed as a batch file but yet is
outputting formatted results as opposed to executable commands alone.
The solution to the command line parameters is to loop through each available
one and with each parameter gather all matching files looping through each of
those. Since we had wanted to act like a built-in command we needed to work
in the batch environment. So to get formatted output we employ the ECHO
command. A feature of that command under JANOS is that quotation marks may
be used to avoid white space trimming that can occur under other operating
systems.
So here is the script for review:
bruce_dev /> cat flash/cksums.bat
<?
function println($s) {
puts("@echo \" ".$s."\"");
};
for ($n = 1; $n < count($_GET); $n++) {
$list = scandir($_GET[$n]);
foreach ($list as $arg) {
if (is_file($arg)) {
$time = filemtime($arg);
println(" file: $arg");
println(" date: $time ".date("UY-m-d H:i:s T", $time));
println(" crc: ".file_crc($arg));
println(" md4: ".file_md4($arg));
println(" md5: ".file_md5($arg));
println(" sha1: ".file_sha1($arg));
println("sha256: ".file_sha2($arg));
println("");
}
}
}
bruce_dev />
The /flash/cksums.bat batch file and therefore this custom command is
provided by default with JNIORs shipped from the factory.
NOTES
There are many ways to accomplish this script. Consider the following
alternative. This eliminates the function
println() that issued text as
an ECHO command and utilizes the JANOS script
printf() feature where the
ECHO is handled in the format string.
<?
$format = "@echo \"%7s: %s\"\n";
for ($n = 1; $n < count($_GET); $n++) {
$list = scandir($_GET[$n]);
foreach ($list as $arg) {
if (is_file($arg)) {
printf($format, "file", $arg);
$time = filemtime($arg);
printf($format, "date", "$time ".
date("UY-m-d H:i:s T", $time));
$content = fread($arg);
printf($format, "crc", crc($content));
printf($format, "md4", md4($content));
printf($format, "md5", md5($content));
printf($format, "sha1", sha1($content));
printf($format, "sha256", sha2($content));
}
}
}
The result is quite the same although this executes a bit faster in that it
reads the file content only once.
bruce_dev /> cksums etc/JanosClasses.jar
file: /etc/JanosClasses.jar
date: 1614613137 2021-03-01 15:38:57 UTC
crc: e352e30a
md4: ca9352ee0b28c7ffc7986ef93c9e489b
md5: 343527bed395496dd31e181895f4b1eb
sha1: b1f8b5676ecfaaac5eb384a3866330add442ef12
sha256: 79c14030548637a7009b4812fcbe50677ed89fa72b115b19ab04f9bf6ff123c8
bruce_dev />
You can execute this script using the
RUN command to examine the command
output before it is interpreted for batch execution. The RUN command can be
helpful in debugging scripts that are meant to be used in this fashion. In this
case you do have to fully specify the script file name since the RUN command
uses the .PRG extension by default.
bruce_dev /> run cksums.bat /flash/jbakup.jar
@echo " file: /flash/JBakup.jar"
@echo " date: 1680189500 2023-03-30 15:18:20 UTC"
@echo " crc: c5ef5f45"
@echo " md4: f07aa62c88cff004c64061f51cc4c87a"
@echo " md5: b8abf72c38da6e409575c9c4304995f9"
@echo " sha1: 911b8b5484c2f467c9a668ded93cacc30a6905af"
@echo " sha256: 634e105d1f734126a6782da0a7449ccaf466d0e88b9eb0e84a4d81181d865497"
@echo " "
bruce_dev />
SEE ALSO
HELP Topics:
ECHO,
CAT,
RUN
[/flash/manpages/scripting.hlp:1781]