mirror of https://github.com/octoleo/hosts.git
Use `local` keyword more consistently.
This commit is contained in:
parent
a09d0fc893
commit
fa920b0a97
29
hosts
29
hosts
|
@ -679,8 +679,7 @@ add() {
|
||||||
else
|
else
|
||||||
if [[ -n ${comment} ]]
|
if [[ -n ${comment} ]]
|
||||||
then
|
then
|
||||||
local formatted_comment
|
local formatted_comment=$(_join " " "${comment[@]}")
|
||||||
formatted_comment=$(_join " " "${comment[@]}")
|
|
||||||
printf "%s\t%s\t# %s\n" \
|
printf "%s\t%s\t# %s\n" \
|
||||||
"${ip}" \
|
"${ip}" \
|
||||||
"${hostname}" \
|
"${hostname}" \
|
||||||
|
@ -713,7 +712,9 @@ disable() {
|
||||||
else
|
else
|
||||||
_debug printf "disable() \$search_term: %s\n" "$search_term"
|
_debug printf "disable() \$search_term: %s\n" "$search_term"
|
||||||
|
|
||||||
targets=$(sed -n "s/^\([^#]*${search_term}.*\)$/\1/p" "${HOSTS_PATH}")
|
local targets=$(
|
||||||
|
sed -n "s/^\([^#]*${search_term}.*\)$/\1/p" "${HOSTS_PATH}"
|
||||||
|
)
|
||||||
_debug printf "disable() \$targets: %s\n" "$targets"
|
_debug printf "disable() \$targets: %s\n" "$targets"
|
||||||
|
|
||||||
printf "Disabling:\n%s\n" "${targets}"
|
printf "Disabling:\n%s\n" "${targets}"
|
||||||
|
@ -774,9 +775,9 @@ enable() {
|
||||||
$_ME help enable
|
$_ME help enable
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
target_regex="s/^\#disabled: \(.*${search_term}.*\)$/\1/"
|
local target_regex="s/^\#disabled: \(.*${search_term}.*\)$/\1/"
|
||||||
|
|
||||||
targets=$(sed -n "${target_regex}p" "${HOSTS_PATH}")
|
local targets=$(sed -n "${target_regex}p" "${HOSTS_PATH}")
|
||||||
printf "Enabling:\n%s\n" "${targets}"
|
printf "Enabling:\n%s\n" "${targets}"
|
||||||
|
|
||||||
# -i '' - in place edit. BSD sed requires extension argument, for GNU
|
# -i '' - in place edit. BSD sed requires extension argument, for GNU
|
||||||
|
@ -824,7 +825,9 @@ Description:
|
||||||
EOM
|
EOM
|
||||||
list() {
|
list() {
|
||||||
# Get the disabled records up front for the two cases where they are needed.
|
# Get the disabled records up front for the two cases where they are needed.
|
||||||
disabled_records=$(sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}")
|
local disabled_records=$(
|
||||||
|
sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}"
|
||||||
|
)
|
||||||
|
|
||||||
if [[ -n "$1" ]]
|
if [[ -n "$1" ]]
|
||||||
then
|
then
|
||||||
|
@ -869,8 +872,10 @@ remove() {
|
||||||
$_ME help remove
|
$_ME help remove
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
local target_records
|
local target_records=$(
|
||||||
target_records=$(sed -n "s/^\(.*${search_string}.*\)$/\1/p" "${HOSTS_PATH}")
|
sed -n "s/^\(.*${search_string}.*\)$/\1/p" "${HOSTS_PATH}"
|
||||||
|
)
|
||||||
|
|
||||||
if [[ -z ${target_records:-} ]]
|
if [[ -z ${target_records:-} ]]
|
||||||
then
|
then
|
||||||
printf "No matching records found.\n"
|
printf "No matching records found.\n"
|
||||||
|
@ -916,8 +921,12 @@ show() {
|
||||||
if [[ -n "$1" ]]
|
if [[ -n "$1" ]]
|
||||||
then
|
then
|
||||||
# Run `sed` before `grep` to avoid conflict that supress `sed` output.
|
# Run `sed` before `grep` to avoid conflict that supress `sed` output.
|
||||||
disabled_records=$(sed -n "s/^\#\(disabled: .*$1.*\)$/\1/p" "${HOSTS_PATH}")
|
local disabled_records=$(
|
||||||
enabled_records=$(grep "^[^#]*$1" "${HOSTS_PATH}")
|
sed -n "s/^\#\(disabled: .*$1.*\)$/\1/p" "${HOSTS_PATH}"
|
||||||
|
)
|
||||||
|
local enabled_records=$(
|
||||||
|
grep "^[^#]*$1" "${HOSTS_PATH}"
|
||||||
|
)
|
||||||
# Output disabled records secondly for better organization.
|
# Output disabled records secondly for better organization.
|
||||||
[[ -n "$enabled_records" ]] && printf "%s\n" "$enabled_records"
|
[[ -n "$enabled_records" ]] && printf "%s\n" "$enabled_records"
|
||||||
[[ -n "$disabled_records" ]] && printf "%s\n" "$disabled_records"
|
[[ -n "$disabled_records" ]] && printf "%s\n" "$disabled_records"
|
||||||
|
|
Loading…
Reference in New Issue