5 ms·
Huh, no mention of "set -e -u"? Do yourself a favor and follow this, too: http://www.davidpashley.com/articles/writing-robust-shell-scripts/ http://www.davidpas
by arnehormann 13y ago
Huh, no mention of "set -e -u"?
Do yourself a favor and follow this, too:
http://www.davidpashley.com/articles/writing-robust-shell-scripts/ http://www.davidpashley.com/articles/writing-robust-shell-sc...
- lysium 13y agoI second that! Every shell script should start with "set -e -u": exit if any command fails (except if called as a condition) or if an unset variable is used. Usually, you never want your script to continue running after a command unexpectedly fails or you use an unset variable. Saves so much time debugging!
- unhammer 13y agoThat's a great article :-) Regarding traps, I struggled for a while with getting a robust and simple way to kill backgrounded scripts on exit. I would have a script do stuff like "sort bigfile & pid=$!; runlongtask; wait $pid", and on Ctrl-C I wanted the sort command to stop too. The trick is to use "kill 0", which kills the non-interactive script and its subprocesses (see "man 2 kill"). So say you want to both remove some temp directory and kill all subprocesses on Ctrl-C, put this at the top of your script: trap 'rm -rf "$tmp"; kill 0' EXIT
- unhammer 13y agoAlso, regarding the lockfile examples, Linux users get a much simpler method using flock: http://mywiki.wooledge.org/BashFAQ/045?highlight=%28flock%29 http://mywiki.wooledge.org/BashFAQ/045?highlight=%28flock%29