mirror of
https://github.com/octoleo/hosts.git
synced 2024-06-03 15:00:50 +00:00
Make sed
ops in enable
/disable
only match exact columns.
This is an application of the approach to `sed` calls that was included
in 94ca8bf
In order to make editing more precise, `sed` regular expressions only
match exact occurrences of the search string within each entry column.
This commit is contained in:
parent
9cb7576bb5
commit
a33b38b204
46
hosts
46
hosts
|
@ -731,9 +731,21 @@ disable() {
|
||||||
else
|
else
|
||||||
_debug printf "disable() \$search_term: %s\n" "$search_term"
|
_debug printf "disable() \$search_term: %s\n" "$search_term"
|
||||||
|
|
||||||
|
target_regex_ip="^\([^#]${search_term}[${_TAB_SPACE_}]..*\)$"
|
||||||
|
target_regex_commented_domain="^\([^#]..*[${_TAB_SPACE_}]${search_term}[${_TAB_SPACE_}]..*\)$"
|
||||||
|
target_regex_domain="^\([^#]..*[${_TAB_SPACE_}]${search_term}\)$"
|
||||||
|
|
||||||
|
# Regular Expression Notes
|
||||||
|
#
|
||||||
|
# - Note double periods in regular expression in order to emulate /.+/,
|
||||||
|
# which apparently doesn't work properly with all versions of sed.
|
||||||
local targets
|
local targets
|
||||||
targets=$(
|
targets=$(
|
||||||
sed -n "s/^\([^#]*${search_term}.*\)$/\1/p" "${HOSTS_PATH}"
|
sed -n \
|
||||||
|
-e "s/${target_regex_ip}/\1/p" \
|
||||||
|
-e "s/${target_regex_commented_domain}/\1/p" \
|
||||||
|
-e "s/${target_regex_domain}/\1/p" \
|
||||||
|
"${HOSTS_PATH}"
|
||||||
)
|
)
|
||||||
_debug printf "disable() \$targets: %s\n" "$targets"
|
_debug printf "disable() \$targets: %s\n" "$targets"
|
||||||
if [[ -z "${targets}" ]]
|
if [[ -z "${targets}" ]]
|
||||||
|
@ -745,7 +757,11 @@ disable() {
|
||||||
|
|
||||||
# -i '' - in place edit. BSD sed requires extension argument, for GNU
|
# -i '' - in place edit. BSD sed requires extension argument, for GNU
|
||||||
# it's optional. More info: http://stackoverflow.com/a/16746032
|
# it's optional. More info: http://stackoverflow.com/a/16746032
|
||||||
sed -i '' "s/^\([^#]*${search_term}.*\)$/\#disabled: \1/g" "${HOSTS_PATH}"
|
sed -i '' \
|
||||||
|
-e "s/${target_regex_ip}/\#disabled: \1/g" \
|
||||||
|
-e "s/${target_regex_commented_domain}/\#disabled: \1/g" \
|
||||||
|
-e "s/${target_regex_domain}/\#disabled: \1/g" \
|
||||||
|
"${HOSTS_PATH}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -799,11 +815,25 @@ enable() {
|
||||||
$_ME help enable
|
$_ME help enable
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
local target_regex="s/^\#disabled: \(.*${search_term}.*\)$/\1/"
|
_debug printf "enable() \$search_term: %s\n" "$search_term"
|
||||||
|
|
||||||
|
target_regex_ip="^\#disabled: \(${search_term}[${_TAB_SPACE_}]..*\)$"
|
||||||
|
target_regex_commented_domain="^\#disabled: \(..*[${_TAB_SPACE_}]${search_term}[${_TAB_SPACE_}]..*\)$"
|
||||||
|
target_regex_domain="^\#disabled: \(..*[${_TAB_SPACE_}]${search_term}\)$"
|
||||||
|
|
||||||
|
# Regular Expression Notes
|
||||||
|
#
|
||||||
|
# - Note double periods in regular expression in order to emulate /.+/,
|
||||||
|
# which apparently doesn't work properly with all versions of sed.
|
||||||
local targets
|
local targets
|
||||||
targets=$(sed -n "${target_regex}p" "${HOSTS_PATH}")
|
targets=$(
|
||||||
_debug printf "enable() \$targets: %s\n" "$targets"
|
sed -n \
|
||||||
|
-e "s/${target_regex_ip}/\1/p" \
|
||||||
|
-e "s/${target_regex_commented_domain}/\1/p" \
|
||||||
|
-e "s/${target_regex_domain}/\1/p" \
|
||||||
|
"${HOSTS_PATH}"
|
||||||
|
)
|
||||||
|
_debug printf "enable() \$targets: %s\n" "$targets"
|
||||||
if [[ -z "${targets}" ]]
|
if [[ -z "${targets}" ]]
|
||||||
then
|
then
|
||||||
_die printf "Not found: %s\n" "${search_term}"
|
_die printf "Not found: %s\n" "${search_term}"
|
||||||
|
@ -813,7 +843,11 @@ enable() {
|
||||||
|
|
||||||
# -i '' - in place edit. BSD sed requires extension argument, for GNU
|
# -i '' - in place edit. BSD sed requires extension argument, for GNU
|
||||||
# it's optional. More info: http://stackoverflow.com/a/16746032
|
# it's optional. More info: http://stackoverflow.com/a/16746032
|
||||||
sed -i '' "${target_regex}g" "${HOSTS_PATH}"
|
sed -i '' \
|
||||||
|
-e "s/${target_regex_ip}/\1/g" \
|
||||||
|
-e "s/${target_regex_commented_domain}/\1/g" \
|
||||||
|
-e "s/${target_regex_domain}/\1/g" \
|
||||||
|
"${HOSTS_PATH}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user