Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ My personal view is that the benefits of `errexit` outweigh its disadvantages. M

### nounset (*set -u*)

By enabling `set -u` (or the equivalent `set -o nounset`) the script will exit if an attempt is made to expand an unset variable. This can be useful both for detecting typos as well as potentially premature usage of variables which were expected to have been set earlier. The controvery here arises in that many Bash scripting coding idioms rely on referencing unset variables, which in the absence of this option are perfectly valid. Further discussion on this option can be found in [BashFAQ/112](https://mywiki.wooledge.org/BashFAQ/112).
By enabling `set -u` (or the equivalent `set -o nounset`) the script will exit if an attempt is made to expand an unset variable. This can be useful both for detecting typos as well as potentially premature usage of variables which were expected to have been set earlier. The controversy here arises in that many Bash scripting coding idioms rely on referencing unset variables, which in the absence of this option are perfectly valid. Further discussion on this option can be found in [BashFAQ/112](https://mywiki.wooledge.org/BashFAQ/112).

This option is enabled for the same reasons as described above for `errexit`.

Expand Down
11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ set -o errtrace # Make sure any error trap is inherited
set -o nounset # Disallow expansion of unset variables
set -o pipefail # Use last non-zero exit code in a pipeline

# Trap to clean up temporary file on exit
trap cleanup EXIT

cleanup() {
if [[ -n ${tmp_file-} && -f ${tmp_file-} ]]; then
rm -f "$tmp_file"
fi
}

# Main control flow
function main() {
# shellcheck source=source.sh
Expand Down Expand Up @@ -55,11 +64,13 @@ function build_template() {
printf '%s\n' "$script_data"
} > template.sh

local tmp_file
tmp_file="$(mktemp /tmp/template.XXXXXX)"
sed -e '/# shellcheck source=source\.sh/{N;N;d;}' \
-e 's/BASH_SOURCE\[1\]/BASH_SOURCE[0]/' \
template.sh > "$tmp_file"
mv "$tmp_file" template.sh
tmp_file="" # Clear variable so cleanup doesn't try to remove it
chmod +x template.sh
}

Expand Down