1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-05-29 04:40:46 +00:00

Assign remove regular expressions to variables.

Since the regular expressions are essentially repeated, with the only
difference being the capturing groups that have no impact on the delete
operations, assign them to a set of descriptive variables. This makes
things more organized and additionally provides some explanation for
what each regular expression is matching against.
This commit is contained in:
William Melody 2015-09-12 16:09:49 -07:00
parent ab9a08d954
commit 95fd43668a

34
hosts
View File

@ -935,9 +935,9 @@ EOM
remove() { remove() {
_verify_write_permissions _verify_write_permissions
local is_search_pair=0 local is_search_pair=0
local search_ip local search_ip=""
local search_hostname local search_hostname=""
local search_string local search_string=""
_debug printf "remove() \$1: %s\n" "${1:-}" _debug printf "remove() \$1: %s\n" "${1:-}"
_debug printf "remove() \$2: %s\n" "${2:-}" _debug printf "remove() \$2: %s\n" "${2:-}"
@ -957,6 +957,14 @@ remove() {
_debug printf "remove() \$search_string: %s\n" "$search_string" _debug printf "remove() \$search_string: %s\n" "$search_string"
fi fi
# IP / Hostname pair regular expressions:
local target_regex_ip_hostname_commented="^\(${search_ip}[${_TAB_SPACE_}]*${search_hostname}[${_TAB_SPACE_}]..*\)$"
local target_regex_ip_hostname="^\(${search_ip}[${_TAB_SPACE_}]*${search_hostname}\)$"
# Search string regular expressions:
local target_regex_ip="^\(${search_string}[${_TAB_SPACE_}]..*\)$"
local target_regex_commented_hostname="^\(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$"
local target_regex_hostname="^\(..*[${_TAB_SPACE_}]${search_string}\)$"
# Regular Expression Notes # Regular Expression Notes
# #
# - Note double periods in regular expression in order to emulate /.+/, # - Note double periods in regular expression in order to emulate /.+/,
@ -967,16 +975,16 @@ remove() {
then then
target_records=$( target_records=$(
sed -n \ sed -n \
-e "s/^\(${search_ip}[${_TAB_SPACE_}]*${search_hostname}[${_TAB_SPACE_}]..*\)$/\1/p" \ -e "s/${target_regex_ip_hostname_commented}/\1/p" \
-e "s/^\(${search_ip}[${_TAB_SPACE_}]*${search_hostname}\)$/\1/p" \ -e "s/${target_regex_ip_hostname}/\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
else else
target_records=$( target_records=$(
sed -n \ sed -n \
-e "s/^\(${search_string}[${_TAB_SPACE_}]..*\)$/\1/p" \ -e "s/${target_regex_ip}/\1/p" \
-e "s/^\(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$/\1/p" \ -e "s/${target_regex_commented_hostname}/\1/p" \
-e "s/^\(..*[${_TAB_SPACE_}]${search_string}\)$/\1/p" \ -e "s/${target_regex_hostname}/\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
fi fi
@ -1016,14 +1024,14 @@ remove() {
if ((is_search_pair)) if ((is_search_pair))
then then
sed -i '' \ sed -i '' \
-e "s/^${search_ip}[${_TAB_SPACE_}]${search_hostname}[${_TAB_SPACE_}]..*$/d" \ -e "/${target_regex_ip_hostname_commented}/d" \
-e "s/^${search_ip}[${_TAB_SPACE_}]${search_hostname}$/d" \ -e "/${target_regex_ip_hostname}/d" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
else else
sed -i '' \ sed -i '' \
-e "/^${search_string}[${_TAB_SPACE_}]..*$/d" \ -e "/${target_regex_ip}/d" \
-e "/^..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*$/d" \ -e "/${target_regex_commented_hostname}/d" \
-e "/^..*[${_TAB_SPACE_}]${search_string}$/d" \ -e "/${target_regex_hostname}/d" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
fi fi
printf "Removed:\n%s\n" "${target_records}" printf "Removed:\n%s\n" "${target_records}"