diff --git a/README.md b/README.md index df4bce6..2e57393 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/build.sh b/build.sh index b88f64e..faa76d9 100755 --- a/build.sh +++ b/build.sh @@ -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 @@ -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 }