From 124520efc49a96fe1e15153203b043bf2d3510ab Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 18 Mar 2015 16:07:49 -0700 Subject: [PATCH] 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. --- Readme.md | 1 + hosts | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 222b4ee..5dca020 100644 --- a/Readme.md +++ b/Readme.md @@ -8,6 +8,7 @@ A command line program with shortcuts for managing hosts file entries. hosts add hosts remove hosts list [] + hosts show ( | | ) hosts edit hosts file diff --git a/hosts b/hosts index 5b29f9e..04f4617 100755 --- a/hosts +++ b/hosts @@ -694,15 +694,15 @@ file() { desc list <] 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 < | | ) + +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 ###############################################################################