diff --git a/Readme.md b/Readme.md index 4d6bbf3..f559c00 100644 --- a/Readme.md +++ b/Readme.md @@ -5,8 +5,9 @@ A command line program with shortcuts for managing hosts file entries. ## Usage Usage: + hosts hosts add - hosts remove + hosts remove ( | | ) [--force] hosts list [enabled | disabled | ] hosts show ( | | ) hosts disable ( | | ) diff --git a/hosts b/hosts index 8ad7cc2..4c54278 100755 --- a/hosts +++ b/hosts @@ -809,19 +809,43 @@ list() { desc remove < + $_me remove ( | | ) [--force] + +Options: + --force Skip the confirmation prompt. Description: - Remove all IP / hostname pairs for a given hostname. + Remove one or more disabled records based on a given IP address, hostname, + or search term. EOM remove() { _verify_write_permissions - local hostname=${1:-} - if [[ -z $hostname ]]; then + local search_string=${1:-} + if [[ -z $search_string ]]; then $_me help remove exit 1 else - sed -i "/^[^#].*[^A-Za-z0-9\.]${hostname}$/d" "${HOSTS_PATH}" + local target_records=$(sed -n "s/^\(.*${search_string}.*\)$/\1/p" "${HOSTS_PATH}") + if ! _command_argv_includes "--force"; then + printf "Removing the following records:\n%s\n" "$target_records" + while true; do + read -p "Are you sure you want to proceed? [y/n] " yn + case $yn in + [Yy]* ) + break + ;; + [Nn]* ) + printf "Exiting...\n" + exit 0 + ;; + * ) + printf "Please answer yes or no.\n" + ;; + esac + done + fi + sed -i "/^.*${search_string}.*$/d" "${HOSTS_PATH}" + printf "Removed:\n%s\n" "${target_records}" fi }