Add `show` command and use that for `list` searches

Displaying a record pair for a hostname or IP address is likely a common
operation, and using the `show` name makes it clear what the primary
function of the command is. Since record pairs are very simple, the
easiest way to allow both hostname and ip address arguments is to use
grep, which also provides general search-like functionality. In order to
avoid doubling this functionality, use the `show` command in the `list`
command for search.
This commit is contained in:
William Melody 2015-03-18 16:07:49 -07:00
parent cdf7793d7e
commit 124520efc4
2 changed files with 23 additions and 4 deletions

View File

@ -8,6 +8,7 @@ A command line program with shortcuts for managing hosts file entries.
hosts add <ip> <hostname>
hosts remove <hostname>
hosts list [<search string>]
hosts show ( <ip> | <hostname> | <search string> )
hosts edit
hosts file

26
hosts
View File

@ -694,15 +694,15 @@ file() {
desc list <<EOM
Usage:
$_me list [127.]
$_me list [<search string>]
Description:
List the existing IP / hostname pairs. When provided with the beginning of
an IP address, lists the pairs with matching IP addresses.
List the existing IP / hostname pairs. When provided with a seach string, all
matching lines will be printed.
EOM
list() {
if [[ -n "$1" ]]; then
grep "^[^#]*$1" /etc/hosts
$_me show "$1"
else
# NOTE: use separate expressions since using a | for the or results in
# inconsistent behavior.
@ -729,6 +729,24 @@ remove() {
fi
}
# ------------------------------------------------------------------------ show
desc show <<EOM
Usage:
$_me show ( <ip> | <hostname> | <search string> )
Description:
Print entries matching a given IP address, hostname, or search string.
EOM
show() {
if [[ -n "$1" ]]; then
grep "^[^#]*$1" /etc/hosts
else
$_me help show
exit 1
fi
}
###############################################################################
# Run Program
###############################################################################