JANOS Help System: [Commands] [Topics] [Tech Support] [Printable Manual] [Search]
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 /> SEE ALSO HELP Topics: ECHO, CAT [/flash/manpages/program.hlp:1456]