From 02a87b09385d75e4b578a52d835ad01af0b72685 Mon Sep 17 00:00:00 2001 From: William Melody Date: Thu, 25 Jun 2015 17:17:12 -0700 Subject: [PATCH] Add optional extension argument to `sed -i` calls BSD sed requires the optional exention argument for the -i option, while in GNU sed it's optional. Including a blank extension for portability. --- hosts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/hosts b/hosts index ab10484..91895de 100755 --- a/hosts +++ b/hosts @@ -647,11 +647,16 @@ disable() { $_ME help disable exit 1 else + _debug printf "disable() \$search_term: %s\n" "$search_term" targets=$(sed -n "s/^\([^#]*${search_term}.*\)$/\1/p" "${HOSTS_PATH}") + _debug printf "disable() \$targets: %s\n" "$targets" + printf "Disabling:\n%s\n" "${targets}" - sed -i "s/^\([^#]*${search_term}.*\)$/\#disabled: \1/g" "${HOSTS_PATH}" + # -i '' - in place edit. BSD sed requires extension argument, for GNU + # it's optional. More info: http://stackoverflow.com/a/16746032 + sed -i '' "s/^\([^#]*${search_term}.*\)$/\#disabled: \1/g" "${HOSTS_PATH}" fi } @@ -708,7 +713,9 @@ enable() { targets=$(sed -n "${target_regex}p" "${HOSTS_PATH}") printf "Enabling:\n%s\n" "${targets}" - sed -i "${target_regex}g" "${HOSTS_PATH}" + # -i '' - in place edit. BSD sed requires extension argument, for GNU + # it's optional. More info: http://stackoverflow.com/a/16746032 + sed -i '' "${target_regex}g" "${HOSTS_PATH}" fi } @@ -815,7 +822,9 @@ remove() { esac done fi - sed -i "/^.*${search_string}.*$/d" "${HOSTS_PATH}" + # -i '' - in place edit. BSD sed requires extension argument, for GNU + # it's optional. More info: http://stackoverflow.com/a/16746032 + sed -i '' "/^.*${search_string}.*$/d" "${HOSTS_PATH}" printf "Removed:\n%s\n" "${target_records}" fi }