Bash trap: Clean Up Temp Files and Locks on Exit
Temp files and stale locks left behind by failed scripts are avoidable. Tested trap patterns for EXIT, INT, TERM and ERR, plus mkdir and flock locks.
Topic
Temp files and stale locks left behind by failed scripts are avoidable. Tested trap patterns for EXIT, INT, TERM and ERR, plus mkdir and flock locks.
set -euo pipefail is good advice, but each option has rules about when it fires. Here is what each one does and the cases that surprise people.
Why I convert os.system() calls to subprocess.run(): no shell injection, real exit codes, captured output, timeouts and input, with notes for Python 3.6.
?->, ?? and ??= all deal with null, but they solve different problems. Tested examples of each, short-circuiting, and the places where ?-> is not allowed.
match looks like a shorter switch, but it compares strictly, never falls through, returns a value and throws when nothing matches. Here is what that means in practice.
A yes/no confirmation prompt for Bash scripts that accepts any case, has an optional default and handles cron, pipes and EOF without looping forever.
Run a command silently and test only its exit status. Why "$@" beats storing a command in a string, and what to use for pipelines.