“Exit traps” can make your Bash scripts more robust and reliable (2013)
111–120 of 159 posts
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#112For me it is "read only".
It is too arcane, even for me.
I use Perl now. I tried to reform last year as I was building a system from lots of executable pieces, the perfect job for Bash
After much pain and suffering I re-wrote it in Perl. What a (relative) breeze.
Just. Don't. Do. Bash.
Works for me!
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#113Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#114Just as an alternative suggestion, consider using a "down file" instead if you can and letting the code gracefully end. Plus you get to write "touch down" and do an end zone dance.
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#115Earlier quoted context omitted.
> Because trap doesn't pass the signal information to the handler You can examine $? on entry to the trap function. On signals, it will be 128 + signal. i.e. on TERM (15) it will be 143. On INT (2) it will be 130. #!/bin/bash skip_exit= on_exit() { code=$? if test $code == 130; then skip_exit=1 fi if test -n "$skip_exit"; then return fi echo "Exiting with: $code" return $code } trap on_exit INT EXIT sleep 2 false Wit…
For robust code you should also use set -e. This changes things again. Often a process started by the shell will get the signal, too (depends of course on how it is sent) and exit with a non-zero return value (depends on the process of course). I believe (not at the computer right now) the handler for EXIT is called in that case. Was it so that bash can also trap ERR, but dash cannot? It's not perfectly easy to handl…
Highly debatable.
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#116Just as an alternative suggestion, consider using a "down file" instead if you can and letting the code gracefully end. Plus you get to write "touch down" and do an end zone dance.
I think you need to be a little more specific about what the "down file" is for, especially since you can't for google it.
while true; do
echo hello
test -f down && exit
doneNow to stop early you execute "touch down".
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#117Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#118Earlier quoted context omitted.
It's secret enough to be well documented in the man page. The real question is, why do people look to random web pages prior to having digested everything in the manual? People used to say "rtfm" all the time, this would be regarded as shockingly rude in today's tech culture but it was a valuable public service to have it repeated, like being reminded to eat your vegetables.
Because ain't nobody got time for that. ;) More seriously, I think that we have been trained to rely on just in time searches (or ChatGPT sessions) when we encounter the next thing we need to learn. RTFM is just so time consuming and I personally don't recall everything I have read, leading me to rely on search/AI to re-learn the next thing just in time anyways. In some ways this is a vast improvement, which is why i…
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#119Don't use traps unless you have to. They are subtly complex and require a great deal more code to deal with edge cases. There is almost always a simpler way to accomplish what you want. If Bash has taught me anything, it's that many advanced features should seldom be used. Always resist the temptation to be fancy.
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#120An annoying thing about bash is that EXIT will also run on SIGINT (^C), which most other shells won't (in my reading it's also not POSIX compliant, although the document is a bit vague). Some might argue this is a feature, but IMHO it's a bug – sometimes you really don't want cleanup to happen so people can inspect the contents of temporary files for debugging. Because trap doesn't pass the signal information to the…
Some temporary file remover. Lol indeed