mirror of https://github.com/octoleo/hosts.git
Simplify "strict mode" section in order to avoid too much noise
These comments can be found in the bash-boilerplate project and aren't necessary here. Since the $DEFAULT_IFS doesn't appear to be used, either, the $IFS setting can be reduced to a since simple assignment.
This commit is contained in:
parent
bcf308ffd3
commit
0830fa43b8
95
hosts
95
hosts
|
@ -27,103 +27,10 @@
|
||||||
# Strict Mode
|
# Strict Mode
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
# Treat unset variables and parameters other than the special parameters ‘@’ or
|
|
||||||
# ‘*’ as an error when performing parameter expansion. An 'unbound variable'
|
|
||||||
# error message will be written to the standard error, and a non-interactive
|
|
||||||
# shell will exit.
|
|
||||||
#
|
|
||||||
# This requires using parameter expansion to test for unset variables.
|
|
||||||
#
|
|
||||||
# http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
|
|
||||||
#
|
|
||||||
# The two approaches that are probably the most appropriate are:
|
|
||||||
#
|
|
||||||
# ${parameter:-word}
|
|
||||||
# If parameter is unset or null, the expansion of word is substituted.
|
|
||||||
# Otherwise, the value of parameter is substituted. In other words, "word"
|
|
||||||
# acts as a default value when the value of "$parameter" is blank. If "word"
|
|
||||||
# is not present, then the default is blank (essentially an empty string).
|
|
||||||
#
|
|
||||||
# ${parameter:?word}
|
|
||||||
# If parameter is null or unset, the expansion of word (or a message to that
|
|
||||||
# effect if word is not present) is written to the standard error and the
|
|
||||||
# shell, if it is not interactive, exits. Otherwise, the value of parameter
|
|
||||||
# is substituted.
|
|
||||||
#
|
|
||||||
# Examples
|
|
||||||
# ========
|
|
||||||
#
|
|
||||||
# Arrays:
|
|
||||||
#
|
|
||||||
# ${some_array[@]:-} # blank default value
|
|
||||||
# ${some_array[*]:-} # blank default value
|
|
||||||
# ${some_array[0]:-} # blank default value
|
|
||||||
# ${some_array[0]:-default_value} # default value: the string 'default_value'
|
|
||||||
#
|
|
||||||
# Positional variables:
|
|
||||||
#
|
|
||||||
# ${1:-alternative} # default value: the string 'alternative'
|
|
||||||
# ${2:-} # blank default value
|
|
||||||
#
|
|
||||||
# With an error message:
|
|
||||||
#
|
|
||||||
# ${1:?'error message'} # exit with 'error message' if variable is unbound
|
|
||||||
#
|
|
||||||
# Short form: set -u
|
|
||||||
set -o nounset
|
set -o nounset
|
||||||
|
|
||||||
# Exit immediately if a pipeline returns non-zero.
|
|
||||||
#
|
|
||||||
# NOTE: this has issues. When using read -rd '' with a heredoc, the exit
|
|
||||||
# status is non-zero, even though there isn't an error, and this setting
|
|
||||||
# then causes the script to exit. read -rd '' is synonymous to read -d $'\0',
|
|
||||||
# which means read until it finds a NUL byte, but it reaches the EOF (end of
|
|
||||||
# heredoc) without finding one and exits with a 1 status. Therefore, when
|
|
||||||
# reading from heredocs with set -e, there are three potential solutions:
|
|
||||||
#
|
|
||||||
# Solution 1. set +e / set -e again:
|
|
||||||
#
|
|
||||||
# set +e
|
|
||||||
# read -rd '' variable <<EOF
|
|
||||||
# EOF
|
|
||||||
# set -e
|
|
||||||
#
|
|
||||||
# Solution 2. <<EOF || true:
|
|
||||||
#
|
|
||||||
# read -rd '' variable <<EOF || true
|
|
||||||
# EOF
|
|
||||||
#
|
|
||||||
# Solution 3. Don't use set -e or set -o errexit at all.
|
|
||||||
#
|
|
||||||
# More information:
|
|
||||||
#
|
|
||||||
# https://www.mail-archive.com/bug-bash@gnu.org/msg12170.html
|
|
||||||
#
|
|
||||||
# Short form: set -e
|
|
||||||
set -o errexit
|
set -o errexit
|
||||||
|
|
||||||
# Return value of a pipeline is the value of the last (rightmost) command to
|
|
||||||
# exit with a non-zero status, or zero if all commands in the pipeline exit
|
|
||||||
# successfully.
|
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
IFS="$(printf '\n\t')"
|
||||||
# Set IFS to just newline and tab at the start
|
|
||||||
#
|
|
||||||
# http://www.dwheeler.com/essays/filenames-in-shell.html
|
|
||||||
#
|
|
||||||
# $DEFAULT_IFS and $SAFER_IFS
|
|
||||||
#
|
|
||||||
# $DEFAULT_IFS contains the default $IFS value in case it's needed, such as
|
|
||||||
# when expanding an array and you want to separate elements by spaces.
|
|
||||||
# $SAFER_IFS contains the preferred settings for the program, and setting it
|
|
||||||
# separately makes it easier to switch between the two if needed.
|
|
||||||
#
|
|
||||||
# NOTE: also printing $DEFAULT_IFS to /dev/null to avoid shellcheck warnings
|
|
||||||
# about the variable being unused.
|
|
||||||
DEFAULT_IFS="$IFS"; printf "%s" "$DEFAULT_IFS" > /dev/null
|
|
||||||
SAFER_IFS="$(printf '\n\t')"
|
|
||||||
# Then set $IFS
|
|
||||||
IFS="$SAFER_IFS"
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Globals
|
# Globals
|
||||||
|
|
Loading…
Reference in New Issue