From 746780fbbe82fd6783984a88c0cb68949655e504 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sun, 15 Apr 2018 15:01:34 -0700 Subject: [PATCH] Use explicit escaping for backslashes in all contexts. ShellCheck SC1117 https://github.com/koalaman/shellcheck/wiki/SC1117 --- hosts | 76 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/hosts b/hosts index f6adea5..ffe274b 100755 --- a/hosts +++ b/hosts @@ -596,9 +596,9 @@ help() { cat <> "${HOSTS_PATH}" - printf "Added:\\n%s\t%s\t# %s\\n" \ + printf "Added:\\n%s\\t%s\\t# %s\\n" \ "${ip}" \ "${hostname}" \ "${formatted_comment}" else - printf "%s\t%s\\n" \ + printf "%s\\t%s\\n" \ "${ip}" \ "${hostname}" >> "${HOSTS_PATH}" - printf "Added:\\n%s\t%s\\n" \ + printf "Added:\\n%s\\t%s\\n" \ "${ip}" \ "${hostname}" fi @@ -789,9 +789,9 @@ disable() { else _debug printf "disable() \${search_string}: %s\\n" "${search_string}" - target_regex_ip="^\(${search_string}[${_TAB_SPACE_}]..*\)$" - target_regex_commented_hostname="^\([^#]..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$" - target_regex_hostname="^\([^#]..*[${_TAB_SPACE_}]${search_string}\)$" + target_regex_ip="^\\(${search_string}[${_TAB_SPACE_}]..*\\)$" + target_regex_commented_hostname="^\\([^#]..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\\)$" + target_regex_hostname="^\\([^#]..*[${_TAB_SPACE_}]${search_string}\\)$" # Regular Expression Notes # @@ -800,9 +800,9 @@ disable() { local targets targets=$( sed -n \ - -e "s/${target_regex_ip}/\1/p" \ - -e "s/${target_regex_commented_hostname}/\1/p" \ - -e "s/${target_regex_hostname}/\1/p" \ + -e "s/${target_regex_ip}/\\1/p" \ + -e "s/${target_regex_commented_hostname}/\\1/p" \ + -e "s/${target_regex_hostname}/\\1/p" \ "${HOSTS_PATH}" ) _debug printf "disable() \${targets}: %s\\n" "${targets}" @@ -816,9 +816,9 @@ disable() { # -i '' - in place edit. BSD sed requires extension argument, for GNU # it's optional. More info: http://stackoverflow.com/a/16746032 sed -i '' \ - -e "s/${target_regex_ip}/\#disabled: \1/g" \ - -e "s/${target_regex_commented_hostname}/\#disabled: \1/g" \ - -e "s/${target_regex_hostname}/\#disabled: \1/g" \ + -e "s/${target_regex_ip}/\\#disabled: \\1/g" \ + -e "s/${target_regex_commented_hostname}/\\#disabled: \\1/g" \ + -e "s/${target_regex_hostname}/\\#disabled: \\1/g" \ "${HOSTS_PATH}" fi } @@ -875,9 +875,9 @@ enable() { else _debug printf "enable() \${search_string}: %s\\n" "${search_string}" - target_regex_ip="^\#disabled: \(${search_string}[${_TAB_SPACE_}]..*\)$" - target_regex_commented_hostname="^\#disabled: \(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$" - target_regex_hostname="^\#disabled: \(..*[${_TAB_SPACE_}]${search_string}\)$" + target_regex_ip="^\\#disabled: \\(${search_string}[${_TAB_SPACE_}]..*\\)$" + target_regex_commented_hostname="^\\#disabled: \\(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\\)$" + target_regex_hostname="^\\#disabled: \\(..*[${_TAB_SPACE_}]${search_string}\\)$" # Regular Expression Notes # @@ -886,9 +886,9 @@ enable() { local targets targets=$( sed -n \ - -e "s/${target_regex_ip}/\1/p" \ - -e "s/${target_regex_commented_hostname}/\1/p" \ - -e "s/${target_regex_hostname}/\1/p" \ + -e "s/${target_regex_ip}/\\1/p" \ + -e "s/${target_regex_commented_hostname}/\\1/p" \ + -e "s/${target_regex_hostname}/\\1/p" \ "${HOSTS_PATH}" ) _debug printf "enable() \${targets}: %s\\n" "${targets}" @@ -902,9 +902,9 @@ enable() { # -i '' - in place edit. BSD sed requires extension argument, for GNU # it's optional. More info: http://stackoverflow.com/a/16746032 sed -i '' \ - -e "s/${target_regex_ip}/\1/g" \ - -e "s/${target_regex_commented_hostname}/\1/g" \ - -e "s/${target_regex_hostname}/\1/g" \ + -e "s/${target_regex_ip}/\\1/g" \ + -e "s/${target_regex_commented_hostname}/\\1/g" \ + -e "s/${target_regex_hostname}/\\1/g" \ "${HOSTS_PATH}" fi } @@ -950,7 +950,7 @@ list() { # Get the disabled records up front for the two cases where they are needed. local disabled_records disabled_records=$( - sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}" + sed -n "s/^\\#disabled: \\(.*\\)$/\\1/p" "${HOSTS_PATH}" ) if [[ -n "${1}" ]] @@ -1039,12 +1039,12 @@ remove() { # which apparently doesn't work properly with all versions of sed. # # 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}\)$" + 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}\)$" + 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}\\)$" local target_records @@ -1052,16 +1052,16 @@ remove() { then target_records=$( sed -n \ - -e "s/${target_regex_ip_hostname_commented}/\1/p" \ - -e "s/${target_regex_ip_hostname}/\1/p" \ + -e "s/${target_regex_ip_hostname_commented}/\\1/p" \ + -e "s/${target_regex_ip_hostname}/\\1/p" \ "${HOSTS_PATH}" ) else target_records=$( sed -n \ - -e "s/${target_regex_ip}/\1/p" \ - -e "s/${target_regex_commented_hostname}/\1/p" \ - -e "s/${target_regex_hostname}/\1/p" \ + -e "s/${target_regex_ip}/\\1/p" \ + -e "s/${target_regex_commented_hostname}/\\1/p" \ + -e "s/${target_regex_hostname}/\\1/p" \ "${HOSTS_PATH}" ) fi @@ -1143,7 +1143,7 @@ show() { # Run `sed` before `grep` to avoid conflict that supress `sed` output. local disabled_records disabled_records=$( - sed -n "s/^\#\(disabled: .*${1}.*\)$/\1/p" "${HOSTS_PATH}" + sed -n "s/^\\#\\(disabled: .*${1}.*\\)$/\\1/p" "${HOSTS_PATH}" ) local enabled_records enabled_records=$(