From 9de0edda0705a9496a227c48786450bf9bf4ec95 Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 18 Mar 2015 15:49:21 -0700 Subject: [PATCH] Add `disable` and `enable` commands These commands make it possible to disable records without fully removing them. To 'disable' a record, it simply is commented out with the following pattern `#disabled: ` prepended to the line. --- Readme.md | 2 ++ hosts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/Readme.md b/Readme.md index 5dca020..b76898f 100644 --- a/Readme.md +++ b/Readme.md @@ -9,6 +9,8 @@ A command line program with shortcuts for managing hosts file entries. hosts remove hosts list [] hosts show ( | | ) + hosts disable ( | | ) + hosts enable ( | | ) hosts edit hosts file diff --git a/hosts b/hosts index 04f4617..7db328f 100755 --- a/hosts +++ b/hosts @@ -660,6 +660,30 @@ add() { fi } +# --------------------------------------------------------------------- disable + +desc disable <||) + +Description: + Disable one or more records based on a given ip address, hostname, or + search term. +EOM +disable() { + local search_term=$1 + if [[ -z "${search_term}" ]]; then + $_me help disable + exit 1 + else + + targets=$(sed -n "s/^\([^#]*${search_term}.*\)$/\1/p" /etc/hosts) + printf "Disabling:\n%s\n" "${targets}" + + sed -i "s/^\([^#]*${search_term}.*\)$/\#disabled: \1/g" /etc/hosts + fi +} + # ------------------------------------------------------------------------ edit desc edit <||) + +Description: + Enable one or more disabled records based on a given ip address, hostname, + or search term. +EOM +enable() { + local search_term=$1 + if [[ -z "${search_term}" ]]; then + $_me help enable + exit 1 + else + target_regex="s/^\#disabled: \(.*${search_term}.*\)$/\1/" + + targets=$(sed -n "${target_regex}p" /etc/hosts) + printf "Enabling:\n%s\n" "${targets}" + + sed -i "${target_regex}g" /etc/hosts + fi +} + # ------------------------------------------------------------------------ file desc file <