From 7597b52ee42bdb41f2d4621b6806201274514d0c Mon Sep 17 00:00:00 2001 From: awanawana <32409620+awanawana@users.noreply.github.com> Date: Wed, 11 Feb 2026 16:02:32 +0800 Subject: [PATCH] Fix typo and improve build.sh error handling - Fix typo in README.md: 'controvery' -> 'controversy' (line 86) - Add cleanup trap in build.sh to properly handle temporary file on exit - Clear tmp_file variable after mv to prevent cleanup from trying to remove the successfully created template.sh --- README.md | 2 +- build.sh | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 }