From e9dec32c9031151e9bd0d3297c454dc2834eb080 Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 18 Mar 2015 17:10:21 -0700 Subject: [PATCH] Display disabled records in `list` when present When disabled records are present, include them in a new section in the default output of `list`. If no disabled records are present, don't include the section. --- hosts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/hosts b/hosts index 6540506..a61c559 100755 --- a/hosts +++ b/hosts @@ -746,13 +746,17 @@ Usage: $_me list [enabled | disabled | ] Description: - List the existing IP / hostname pairs. When provided with a seach string, all - matching lines will be printed. + List the existing IP / hostname pairs, optionally limited to a specified + state. When provided with a seach string, all matching enabled records will + be printed. EOM list() { + # Get the disabled records up front for the two cases where they are needed. + disabled_records=$(sed -n "s/^\#disabled: \(.*\)$/\1/p" /etc/hosts) + if [[ -n "$1" ]]; then if [[ "$1" == disabled ]]; then - sed -n "s/^\#disabled: \(.*\)$/\1/p" /etc/hosts + printf "%s\n" "${disabled_records}" elif [[ "$1" == enabled ]]; then grep -v -e '^$' -e '^\s*\#' /etc/hosts else @@ -762,6 +766,9 @@ list() { # NOTE: use separate expressions since using a | for the or results in # inconsistent behavior. grep -v -e '^$' -e '^\s*\#' /etc/hosts + if [[ -n "${disabled_records}" ]]; then + printf "\nDisabled:\n%s\n" "${disabled_records}" + fi fi }