Эх сурвалжийг харах

Update bash strict mode docs

Weiyi Lou 10 жил өмнө
parent
commit
2005aeb76f

+ 1 - 1
vim/settings/templates.vim

@@ -1,6 +1,6 @@
 " Template for shell scripts.
 " Removes comments after line 1, places cursor at end of file.
-autocmd BufNewFile *.sh 0r ~/dotfiles/vim/templates/bashscript | 2,$ g/^#/d | normal G
+autocmd BufNewFile *.sh 0r ~/dotfiles/vim/templates/bashscript | silent 2,$ g/^#/d | normal G
 
 " Make shell scripts executable on save.
 autocmd BufWritePost *.sh if getline(1) =~ "^#!/bin/" | silent !chmod +x <afile>

+ 21 - 12
vim/templates/bashscript

@@ -1,20 +1,29 @@
 #!/bin/bash
 #
-# The following comments are all removed when loading this template:
+# The Unofficial "Bash Strict Mode".
 #
-# The Unofficial "Bash Strict Mode"
-# -e           Failed commands exit
-# -u           Unbound variables exit
-# -o pipefail  Do not hide piped-command failures
-# IFS as newline and tab are better for loops (and name with spaces).
+# -e           Failed commands exit.
+# -u           Unbound variables exit.
+# -o pipefail  Do not hide piped-command failures.
+# IFS=$'\n\t'  Newline/Tab separators are good for loops and names with spaces.
 #
 # Tips:
-# - Options can be switched off and on as required:
-#   - Toggle `-e` if previous command's return value is needed (`$?`).
-#   - Toggle `-u` if sourcing another non-conforming script.
-# - Use default values for positional parameters i.e. `${1:-}`.
-# - Intentionally undefined variables should be set to "" rather than not set.
-# - Learn about "exit traps" to perform `try-catch-finally`-like cleanup tasks
+#
+# 1. Avoid unset (unbound) variables. Use default values for:
+#   - Positional parameters e.g. `${1:-}`.
+#   - Potentially undefined variables e.g. `${ENVVAR:-}`.
+#   - Intentionally empty/undefined variables e.g. `MAYBE_EMPTY=""`.
+#
+# 2. Failed Commands will stop the script. Avoid this by affixing ` || true`.
+#
+# 3. Strict Options can be toggled, if really required:
+#   - Switch off: `set +opt`. Switch back on afterwards: `set -opt`.
+#   - Toggle `-e` if the previous return value (`$?`) is needed.
+#   - Toggle `-u` if sourcing a non-conforming script.
+#
+# 4. Exit Traps can perform try-catch-finally-like clean-up tasks.
+#
+# (These comments are all removed when vim loads this template.)
 #
 set -euo pipefail
 IFS=$'\n\t'