Using `sudo` within a script is generally considered poor practice, so
by default an error message is printed when the user attempts to perform
a write operation without sufficient permissions.
One way to deal avoid this error message is to alias `hosts` to `sudo
hosts`, but this then requires `sudo` for all operations and not just
write operations.
The new `--auto-sudo` option flag provides a way to automatically invoke
a write command with `sudo` when the user doesn't have write
permissions.
In order to provide cleaner documentation for this option, include more
option documentation in README.md.
When `$*` is specified with quotes and braces as `"${*}"`, a blank
argument array results in an error in older versions of bash. Remove
the braces to avoid this error.
Braces are only required in certain cases, but the cognitive overhead in
keeping track of which cases require braces can be reduced by simply
always using them.
Example: `${NAME}`
Retain more widely-used braces `$NAME` convention in documentation.
The `[[ ... ]] && ...` conditional style results in non-zero exits when
the test is false. Moving this to a traditional `if..then` style avoids
this behavior while also being more explicit about the objective of the
code. `|| return 0` or `|| exit 0` could have been added as an
alternative way to avoid the non-zero exit behavior, but is not used in
this case because the traditional `if..then` style is more common.
The `--force` option is passed to the function in `$_COMMAND_ARGV`,
which means that assigning arguments from positions in this array leads
to unexpected behavior depending on where in the argument list `--force`
is included. As a result, the `remove` function must parse the
arguments, removing the `--force` argument before assigning function
arguments to local variables.
resolves#2
The example assignment should have balanced quotes to avoid any copy /
paste errors or reader confusion. Remove the 'not' since it's clearer if
this is used as a parenthetical example of slicing.
Use `unset` to remove the first element of `$_COMMAND_PARAMETERS` rather
than slicing because under bash 3.2 the resulting slice is treated as a
quoted string and doesn't easily get coaxed back into an array.
Since the regular expressions are essentially repeated, with the only
difference being the capturing groups that have no impact on the delete
operations, assign them to a set of descriptive variables. This makes
things more organized and additionally provides some explanation for
what each regular expression is matching against.
This provides a mechanism for removing exact IP and hostname pairs.
The existing search string functionality should continue to function as
it did previously.
The `disable` regular expressions avoid matching against commented lines
by excluding '#' characters at the beginning of the line. However, the
IP portion of the record is at the beginning of non-commented lines, so
in order to match these IPs the `target_regex_ip` needs to have the
search term checked against the beginning of the line. Therefore, the
`[^#]` at the beginning of `target_regex_ip` must be removed to make
`search_term` properly match these IPs.
These variables are used within regular expressions, but as the program
evolves they are not always in constant use. Exporting these variables
silences ShellCheck warnings and makes them available to subshells.
This is an application of the approach to `sed` calls that was included
in 94ca8bf
In order to make editing more precise, `sed` regular expressions only
match exact occurrences of the search string within each entry column.
* master:
Exit with "Not found" message on failed search in `enable`/`disable`
Bump version to 1.5.0
Print confirmation feedback in `add` on success.
Declare and assign separately to avoid masking return values.
Add `brew tap` command to `brew install` in Readme.
In order to make editing more precise, sed regular expressions only
match exact occurrences of the search string within each entry column.
In order to properly handle tab and space separators in a portable
manner, a set of global variables are included that provide strings
of those characters.