Tags: bashtools/JSONPath.sh
Tags
use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh… … for macOS, fix command line parsing, make JSONPath.sh and test scripts robust (#16) * change use of egrep to grep -E While we disagree with the notion that `egrep` is obsolete, tools such as gnu egrep whine when used with the warning: ``` egrep: warning: egrep is obsolescent; using ggrep -E ``` Sadly this pull request changes use of `egrep` to `grep -E` in order to stop the warnings that gnu egrep now issues. * add makefile to install Add a Makefile so that `make install` will work. Updated `README.md` to mention the `make install` alternative. Removed trailing whitespace from `README.md`. * add -S, -A, -T options We add a `-S` option to print spaces around :s'. The default (w/o -S): ```json "submit_slot":2, ``` With `-S`: ```json "submit_slot" : 2, ``` We add an `-A` option start a JSON array on line as JSON member. The default (w/o -A): ```json "authors": [ ``` With `-A`: ```json "authors":[ ``` With `-S -A`: ```json "authors" : [ ``` We add `-T` to indent with tabs. The default (w/o -T): ```json "submit_slot":2, "author_count":3, ``` With `-T`: ```json "submit_slot":2, "author_count":3, ``` We also removed a few trailing whitespace from the shell script. * fix homebrew paths for macOS in ensure_deps.sh For macOS (Darwin) we use use the correct paths for homebrew in `ensure_deps.sh` If there is an error under macOS, we also remind the user what to put in their $PATH. * fix and improve command line parsing and usage The command line parsing now uses the bash builtin `getopts`. Now, and invalid option is flagged as an error. Improper arguments to command line options are flagged as errors. The optional pattern is also obtained after parsing command line options. The usage message has been updated to include command line options that were implemented but not documented in the usage message. Updated the Invocation section of `README.md` to reflect the current command line, including command line options that were documented in the usage message, but not in `README.md`. * fix test command lines The original usage message and `README.md` invocation has the optional pattern at the end of the command line . We fix 3 tests to place the pattern at the end of the command line. Moreover, we put `--` between command line options and a pattern to prevent the pattern from being interpreted as a command line option. And while unlikely to happen, this would allow pattern that begins with a dash to be correctly parsed. Ran `all-tests.sh` to test the above. * improve test scripts We ran into some issues with the test scripts that were fixed by some shell script hardening and using some bash best practices. What follows are fixes and improvements to the test scripts. We make the tests scripts more robust: * detect when the cd fails * prevent unquoted shell variable expansion from corrupting execution We improve the use of bash: * use `env(1)` in the initial `#!` line to find `bash(1)` on `$PATH` NOTE: macOS has deprecated the distributed `bash(1)` in favor of `zsh(1)`. Not all systems have `bash(1)` as `/bin/sh`. We allow for `bash(1)` to come from home brew or mac ports, or ... We use bash constructs for slight speed gain and best practices: * increment using ((++i)), in some cases avoiding a sub-shell launch * bypass use of `cat(1)` by using `$(< "$argpfile")` * use `find(1)` to select test files instead of the slower `ls(1)` * avoid using shell glob problems by using `find(1)` to select test files * use `$(...)` instead of `\`cmd\`` to help avoid data corrupting execution Use `python3(1)` instead of just `python(1)`. NOTE: Not all systems that have `python3(1)` have `python(1)`. Some system admins have yet to select the default version of python. The macOS platform, while they have `python3(1)` to not have `python(1)`. The test scripts now pass the shellcheck script analysis tool. See [www.shellcheck.net](https://www.shellcheck.net) for details. Fixed a bug in `all-docker-tests.sh` where Centos tests were re-displayed a 2nd time as if they were Debian tests. Ran `./all-tests.sh` to verify the above under macOS and RHEL 9. TODO: Fix a number of shell script problems with `JSONPath.sh` along similar lines as above: especially where shell variables containing JSON and other test data can take on values that can corrupt the shell script execution. We wanted to make the other test scripts more robust before taking on `JSONPath.sh` so that we may use such tests. * improve JSONPath.sh We make `JSONPath.sh` much more robust and use bash constructs for slight speed gain and best practices: * prevent unquoted shell variable expansion from corrupting execution * compute numerical values using ((var=equation)) instead of let * declare local variable before assigning a value * fixed how options to grep are accumulated * fixed how main is called * fixed how an unexpected EOF with token is thrown * fixed how array elements are unset * fixed how code determines of an array contains only empty values * changed cases where `cat(1)` of a file is piped into feeding the file on stdin * use `$(...)` instead of `\`cmd\`` to help avoid data corrupting execution * replaced use of `[ ... ]` with `[[ ... ]]` * removed shell variables that were set but never unused * fixed cases where a case statement lacked a default case * fixed how the literal string `'"*'` is used * fixed cases where a scalar is followed by a literal string that starts with `[` * fixed cases where `$@` is confused with `$*` All errors, warnings and issues reported by [shellcheck](https://www.shellcheck.net) have been addressed / fix. Now `JSONPath.sh` is error, warning and issue free under [shellcheck](https://www.shellcheck.net)! Ran `./all-tests.sh` to verify the above under macOS and RHEL 9 Linux.
PreviousNext