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.
This commit is contained in:
William Melody 2015-03-18 17:10:21 -07:00
parent 7d8a94d4ca
commit e9dec32c90
1 changed files with 10 additions and 3 deletions

13
hosts
View File

@ -746,13 +746,17 @@ Usage:
$_me list [enabled | disabled | <search string>]
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
}